pythonextractarcpyarcmap

Using a list to name output files in Arcpy


I am working with NLCD data set and am trying to extract changed pixels based on county boundaries (extract by mask). I have wrote a little script that will do the job BUT i was wondering if somehow I could use the boundary names as my output names because at the moment, the output is just 1, 2, 3, ... As you will see, I am using a counter. I tried counter = ctyList but it doesn't work. Here is the script:

import os
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:\Users\sr\Desktop\Sam\AllCounties"
ctyList = arcpy.ListFeatureClasses ("*.shp")
inRaster = "nlcdrecmosaic"
counter = 1
for shp in ctyList:
 out= arcpy.sa.ExtractByMask(inRaster,shp)
 out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ str(counter))
 counter = counter + 1     

Thank you very much for your help. Sam


Solution

  • for shp in ctyList:
        county_name = shp.split('.')[0]  # Split the file name at the '.' to get just the name without the extension 
        out= arcpy.sa.ExtractByMask(inRaster,shp)
        out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ county_name