11

I know there has to be a way to do this but I'm having trouble figuring out how to do so. I would like to create a list of feature classes to perform operations on however, I want to have the list contain feature classes which have 2 separate and distinct file name identifiers. The code would look something like:

FCS = arcpy.ListFeatureClasses("X_*" OR "*_Y")

I know OR is incorrect however, is there some way to do this without having to re write code to work with the second wildcard. Thanks for helping gout, i know there is a selection method in the Select By Attributes which allows for multiple wildcard use using the OR statement.

  • The wildcard parameter only takes a string as input, so short of running ListFeatureClasses() twice (which could potentially be too slow for large directories, it's best to pass no wildcard and then just filter through the result after. – Paul Jul 25 '14 at 15:54
  • Paul, that is exactly what the list comprehension answer below does - it runs through the list and filters it based on the if statement. – dklassen Jul 25 '14 at 15:58
  • @dklassen, OP is looking for FC that end in "_Y", not start. You can wildcard that against a GDB, but not shapefiles with your code. – Paul Jul 25 '14 at 16:01
15

Add the two together, they are just lists.

FCS = arcpy.ListFeatureClasses("X_*") + arcpy.ListFeatureClasses("*_Y")

To eliminate duplicates:

FCS = set(arcpy.ListFeatureClasses("X_*") + arcpy.ListFeatureClasses("*_Y"))
  • This works also, thanks @Jason Sheirer, using startswith and endswith works for me in a file GDB. When I altered the script I was using to work with an SDE, it didn't work for some reason. The method you mentioned does work in SDE. – standard Jul 30 '14 at 16:14
6

You could approach it a bit differently:

import arcpy
import os

arcpy.env.workspace = 'c:\temp'
fcs = [fc for fc in arcpy.ListFeatureClasses() if fc.startswith('X_') or os.path.splitext(fc)[0].endswith('_Y')]
  • This won't really work unless you are in a GDB. The extension is returned for shapefiles. – Paul Jul 25 '14 at 15:51
  • This should work for any feature class within your set workspace. Including shapefiles - why wouldn't we want an extension returned? – dklassen Jul 25 '14 at 15:54
  • Because you can't wildcard with this against ending characters - which is what OP looks to be doing. – Paul Jul 25 '14 at 16:01
  • @Paul Modified - thanks for the correction. I didn't see the "_Y" part. – dklassen Jul 25 '14 at 16:07
  • 2
    A bit nitpicky, but you should use os.path.splitext(). The shapefile could have a period in the name. +1 – Paul Jul 25 '14 at 16:08
1

This scripts specifies multiple wildcards in a list cases []. Than it loops over unique cases, listing desired feature classes 'arcpy.ListFeatureClasses()'. Each selected feature class is that added to the new list of feature classes using '.extend'

# Create a list of the wildcards 
cases = ["X_*", ""*_Y""]

# Create empty list to store all selected files
fcs= []

# Loop through files to select files following multiple wildcards
for case in cases:
    fc = arcpy.ListFeatureClasses(case)
    # Insert the fc to the list
    fcs.extend(fc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.