Let's say you have a list of shapefiles and the field name for the attribute is the same in each shapefile. That means you can make the SQL query in the beginning of the script, so the first bit could look like this:
search_id = arcpy.GetParameterAsText(0)
shps = [r"path\to\shp1.shp",r"path\to\shp2.shp"]
output_shp = r"path\to\output.shp"
field = "ID_FIELD_NAME"
sql = '"{0}" = \'{1}\''.format(field,search_id)
Here are two ways to go about your selection process. The first uses a where clause in the MakeFeatureLayer() and GetCount(), and the second uses a SearchCursor() to find the attribute and proceeds to MakeFeatureLayer(). I'm not sure which one is faster, but my guess is the second. Most likely, the speed is very similar if you don't have tons of shapefiles.
for shp in shps:
# make feature layer
if arcpy.Exists("fl"):
arcpy.management.Delete("fl")
fl = arcpy.management.MakeFeatureLayer(shp,"fl",sql)
# check count of features in new feature layer, skip to next shp if it == 0
if int(arcpy.management.GetCount(fl).getOutput(0)) == 0:
continue
# copy features, only happens if there is a feature in the feature layer
arcpy.management.CopyFeatures(fl,output_shp)
and here's the second. In this one, a SearchCursor is used to check for the presence of the feature, and then the MakeFeatureLayer() is used. I think this would probably be the faster way:
for shp in shps:
# use list comprehension with a SearchCursor to check for the id
all_ids = [r[0] for r in arcpy.da.SearchCursor(shp,field)]
if not search_id in all_ids:
continue
# now make feature layer and copy features
if arcpy.Exists("fl"):
arcpy.management.Delete("fl")
fl = arcpy.management.MakeFeatureLayer(shp,"fl",sql)
# copy features
arcpy.management.CopyFeatures(fl,output_shp)
EDIT:
With differing field names, you can store them and iterate the shapefiles like this:
shps = [(r"path\to\shp1.shp","ID_FIELD_NAME1"),
(r"path\to\shp2.shp","ID_FIELD_NAME2")]
output_shp = r"path\to\output.shp"
for item in shps:
shp = item[0]
field = item[1]
sql = '"{0}" = \'{1}\''.format(field,search_id)
#continue with the rest of the code as above
You can use Select (Analysis) to perform this operation. This method has the added benefit of preserving your attributes. There are two main steps:
- Use a generator to list all of the unique city codes
- Loop through unique cities codes and add the code to the Select SQL
expression
import arcpy, os
# Define the output workspace
outws = r'C:\temp\out'
# Set the input shapefile
shp = r'C:\temp\test.shp'
# Get a list of unique city codes using a generator
cities = set(row[0] for row in arcpy.da.SearchCursor(shp, "city"))
# Select unique cities and export to new shapefiles
for c in cities:
out_fc = os.path.join(outws, c) # Define the output name
where_clause = '"city" = \'%s\'' % c # Select based on city name
arcpy.Select_analysis(shp, out_fc, where_clause) # Perform the Select
Best Answer
I'd say that it's exporting but to the same directory and then overwriting or tripping up on the first iteration of
listdir
.. best to do all of it in one pass oflistdir
:When you use os.listdir() it will return all files and folders in the given path, your use of os.path.isdir() is incorrect and should be done like:
First for checking if something exists and then finding out if the existing object is a folder, not for checking if a folder (path) exists.