Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.
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.

share|improve this question
add comment

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)
share|improve this answer
add comment

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.

share|improve this answer
    
Thanks. But it doesn't work... –  titi Jun 12 '13 at 18:57
add comment
up vote 2 down vote accepted

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)
share|improve this answer
1  
Please mark this as the "Answer" since you identified the issue. Thanks. –  RyanDalton Jun 12 '13 at 21:30
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.