4
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.

whuber
70.4k17 gold badges189 silver badges285 bronze badges
asked Jun 11, 2013 at 20:28

4 Answers 4

7

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)
answered Jun 11, 2013 at 21:29
4

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.

answered Jun 11, 2013 at 20:36
1
  • Thanks. But it doesn't work... Commented Jun 12, 2013 at 18:57
2

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)
RyanKDalton
23.3k19 gold badges114 silver badges180 bronze badges
answered Jun 12, 2013 at 19:38
0
2

Without specifying .tif, the default raster type is an Esri GRID. An Esri GRID file name cannot exceed 13 characters.

answered Jun 14, 2013 at 17:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.