raslist=arcpy.ListRasters()
for ras in raslist:
arcpy.ProjectRaster_management(ras,outfolder2+ras+'_Albers',spatialref2)
print ras+' has been reprojected'
I was using the above code for batch processing in "Project Raster". I want to add "_Albers" in the output file name for each file. But the third line doesn't work. if I delete the "_Albers" in the third line, it works. But I want to rename the output file.
Thanks.
4 Answers 4
Since you didn't share what "outputfolder2" variable is, you may have been forgetting to include the folder separator (ie- "\").
To use a bit more of a "Pythonic" way of creating your path/filename where you have to remember to force a folder/file separator, use os.path.join
instead of the +
, but I also like using the strategy @David suggested:
import os
raslist=arcpy.ListRasters()
for ras in raslist:
outRaster = os.path.join(outfolder2, ras + "_Albers")
arcpy.ProjectRaster_management(ras, outRaster, spatialref2)
print str(ras) + " has been reprojected to: " + str(outRaster)
Try something like this. It's always cleaner IMO to define the naming outside the actual tool, and that is where your error is here. This way you can also test whether the output file name is okay by e.g. printing it before you run the tool.
raslist=arcpy.ListRasters()
for ras in raslist:
outRaster = outfolder2 + ras + "_Albers"
arcpy.ProjectRaster_management(ras, outRaster, spatialref2)
print str(ras) + " has been reprojected"
When printing a variable in combination with a string like in your last line you need to cast the variable to a string too btw.
-
Thanks. But it doesn't work...titi– titi2013年06月12日 18:57:23 +00:00Commented Jun 12, 2013 at 18:57
The output file was missing the file extension (.tif
). Now it works.
raslist=arcpy.ListRasters()
for ras in raslist:
outRaster = outfolder2 + ras + '_Albers'+'.tif'
arcpy.ProjectRaster_management(ras, outRaster, spatialref2)
print str(ras)+' has been reprojected to:'+ str(outRaster)
Without specifying .tif, the default raster type is an Esri GRID. An Esri GRID file name cannot exceed 13 characters.