I've written the following arcpy script to loop through a directory and intersect all the shapefiles in it with a certain other shapefile:
import arcpy, os
from arcpy import env
from arcpy.sa import *
#To overwrite output
arcpy.env.overwriteOutput = True
#Set environment settings
env.workspace = "F:/Tiled_Shapes/"
outws="F:/Clipped_Shapes/"
#checkout ArcGIS spatial analyst extension license
arcpy.CheckOutExtension("Spatial")
inshape = arcpy.ListDatasets("*","All")
clipfile = "F:/Clip_subset.shp"
for i in inshape:
filename=os.path.splitext(i)[0]
outPolygons= outws + str(filename) + '.shp'
inFeatures = [i,clipfile]
arcpy.Intersect_analysis(in_features=inFeatures,join_attributes = ALL, out_feature_class=outPolygons)
Whats unnerving, is that rather than getting an error, nothing happens. What am I doing wrong here? When I use the intersect feature on the same data in ArcMap, it works fine. Now when I try to do it to a whole directory its failing.
1 Answer 1
In my experience, with arcpy list functions, it's always best to choose the most restrictive one (e.g. if you want to list rasters, use arcpy.ListRasters()
, not arcpy.ListFiles()
.
Even though a shapefile is a dataset, they're not included in the possible formats for arcpy.ListDatasets()
. So here, you should use arcpy.ListFeatureClasses()
(technically a shapefile is a feature class, even if this term is mostly used for geodatabase feature classes).
And as @Midalvo mentioned, it's a good idea to use print
statements to see what's returned by a list (or any function), when you don't get the expected results
inshape = arcpy.ListDatasets("*","All")
line, add aprint inshape
to see what's in the list