Copying rasters to new geodatabase question

1477
3
Jump to solution
03-30-2017 08:27 AM
StephenTham
New Contributor II

I am new to ArcPy and python.  I am currently working on exercise 9.2 in Python Scripting for ArcGIS by Paul Zanbrtgen.  The question is pretty simple and is asking to write a scrip that copies rasters into a new Geodatabase.  I run print statements and everything is fine until I reach the  arcpy.CopyRaster_management(raster, outraster).  I am wondering if there is something wrong with the formatting of the file names. It throws a 999999 error.  How long did it take you to really become comfortable in the Arcpy environment and with python in general. My goal is to build python based geoprocessing tools one day. 

import arcpy
from arcpy import env
out_path = "C:/EsriPress/Python/Data/Exercise09"
env.workspace = out_path
rasterlist = arcpy.ListRasters()
arcpy.CreatePersonalGDB_management(out_path + "/Results", "myrasters.gdb")
for raster in rasterlist:
    desc = arcpy.Describe(raster)
    rname = desc.baseName
    outraster = out_path + "/Results/myrasters.gdb/" + rname
    arcpy.CopyRaster_management(raster, outraster)
0 Kudos
1 Solution

Accepted Solutions
JayantaPoddar
MVP Esteemed Contributor

I am not a Python guy, but a glance on your script, you are creating a Personal Geodatabase with extension .GDB

1. Either change the extension to MDB (i.e. myrasters.mdb)

2. or use arcpy.CreateFileGDB_management

The second one is more preferable.

View solution in original post

3 Replies
JayantaPoddar
MVP Esteemed Contributor

I am not a Python guy, but a glance on your script, you are creating a Personal Geodatabase with extension .GDB

1. Either change the extension to MDB (i.e. myrasters.mdb)

2. or use arcpy.CreateFileGDB_management

The second one is more preferable.

View solution in original post

IanMurray
Frequent Contributor

Good for you picking up python and arcpy.  I took a class in grad school in python and arcpy and have used that same book myself before.  I didn't get too comfortable with it though til I found real applications for its use where I work and its been quite the timesaver for me. 

Now to your questions.

You are getting the base name property of the raster but not an extension(file type).  Your output from Copy Raster will need a file type or it will default to a GRID file which has a 13 character limit and limits the character types that can be in the name.  This is likely causing your issue.

Also when joining paths and file names, I recommend using the os module and os.path.join to combine your path and name instead of concatenation. 

https://docs.python.org/2/library/os.path.html

0 Kudos
StephenTham
New Contributor II

IT was so simple, Thanks for your help!. I just changed it to CreateFileGDB management.  Thanks for all your help! Hopefully as I see more python, ill pick up on this more. I am working backwards through these exercises to I can see how python fits together.