Copy rasters through python scripting

7842
2
Jump to solution
12-20-2014 05:52 AM
AhsanAbbas
New Contributor III

I want to copy all the rasters from folder in to Geodatabase with same name how can i do it, i tried through given script but does not copy with it's original name, what is problem in my code ? please help me ?

import arcpy

from arcpy import env

env.workspace = "      "

rasterlist = arcpy.ListRasters()

for raster in rasterlist:

    desc = arcpy.Describe(raster)

    base_ext = desc.extension

    if base_ext == '' ":

        name = str(desc.basename)

        arcpy.CopyRaster_management(name, CopyResults.gdb/name","DEFAULTS","0","9","","","8_BIT_UNSIGNED")

    else:

        name = str(desc.basename) + "." + str(desc.extension)

        name1 = str(desc.basename)

        arcpy.CopyRaster_management(name, CopyResults.gdb/name1","DEFAULTS","0","9","","","8_BIT_UNSIGNED")

    print name

   

print "Copy all rasters successfully."

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

First of all you have set your workspace to nothing!

env.workspace = "      "

This will make your ListRaster() function fail.

You then have incorrect syntax in the CopyRaster lines. For example you have:

arcpy.CopyRaster_management(name, CopyResults.gdb/name1","DEFAULTS","0","9","","","8_BIT_UNSIGNED")

It should be:

outname = "CopyResults.gdb/" + name1
arcpy.CopyRaster_management(name, outname ,"DEFAULTS","0","9","","","8_BIT_UNSIGNED")

View solution in original post

2 Replies
DuncanHornby
MVP Notable Contributor

First of all you have set your workspace to nothing!

env.workspace = "      "

This will make your ListRaster() function fail.

You then have incorrect syntax in the CopyRaster lines. For example you have:

arcpy.CopyRaster_management(name, CopyResults.gdb/name1","DEFAULTS","0","9","","","8_BIT_UNSIGNED")

It should be:

outname = "CopyResults.gdb/" + name1
arcpy.CopyRaster_management(name, outname ,"DEFAULTS","0","9","","","8_BIT_UNSIGNED")
AhsanAbbas
New Contributor III

Actually this env.workspace = "      " means i defined the workspace, not the empty workspace.

Well the last two lines are really helpful, Thanks.

0 Kudos