A handy wildcard tip

As you know, the arcpy module provides functions built specifically for listing data so it can be iterated through during processing. All these list functions can be passed a wildcard argument which filters what is returned. For example, if you only require feature classes that start with a prefix of “tax”, you would do this:

# Set the workspace. List all feature classes that start with 'tax'.
arcpy.env.workspace = "d:/stjohns/localgovernment.gdb"
fcs = arcpy.ListFeatureClasses("tax*")

However, what if you wanted to retrieve all feature classes which do not start with “tax”? Here is a handy trick for doing it:

arcpy.env.workspace = "d:/stjohns/localgovernment.gdb"
fcs = [fc for fc in arcpy.ListFeatureClasses() if not fc.startswith('tax')]

If “tax” was a suffix, use endswith:

fcs = [fc for fc in arcpy.ListFeatureClasses() if not fc.endswith('tax')]
Advertisements