[GIS] How to rename a file in the output (ArcGIS python)

arcpypython

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.

Best Answer

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)