I'm trying to mask a group of raster files and save the new rasters in a different folder using the same name of the original files.
It doesn't sound as a difficult task, but I'm having trouble saving-naming each masked raster inside the "for" loop. Here is what I have so far:
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/rasters/threshold"
mask = "C:/GIS/mesoamerica.tif"
rasterlist = arcpy.ListDatasets("*", "Raster")
for i in rasterlist:
outExtractByMask = ExtractByMask(i, mask)
outExtractByMask.save("C:/SIG/MelasCA_30runs_avg/threshold/mesoamerica/" i)
I need to find a way to use the "i" variable in the outExtractByMask.save. Any idea?
1 Answer 1
Here is one way to accomplish the task. The general idea here is to create a full output file path using os.path.join()
and input that into the .save
command
import arcpy, os
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/rasters/threshold"
outws = "C:/SIG/MelasCA_30runs_avg/threshold/mesoamerica"
mask = "C:/GIS/mesoamerica.tif"
rasterlist = arcpy.ListDatasets("*", "Raster")
for i in rasterlist:
outExtractByMask = ExtractByMask(i, mask)
outname = os.path.join(outws, str(i)) # Create the full out path
outExtractByMask.save(outname)