Recursive list feature classes

UPDATE: At 10.1 service pack 1, this can be more easily achieved using arcpy.da.Walk. Also see Inventorying data.

The following function (recursive_list_fcs) will return a list of all feature classes that fall under the input workspace, including those not seen by the file system such as in a file geodatabase.

import os
import arcpy

def recursive_list_fcs(workspace, wild_card=None, feature_type=None):
    """Returns a list of all feature classes in a tree.  Returned
    list can be limited by a wildcard, and feature type.
    preexisting_wks = arcpy.env.workspace
    arcpy.env.workspace = workspace

        list_fcs = []
        for root, dirs, files in os.walk(workspace):
            arcpy.env.workspace = root
            fcs = arcpy.ListFeatureClasses(wild_card, feature_type)
            if fcs:
                list_fcs += [os.path.join(root, fc) for fc in fcs]

            # Pick up workspace types that don't have a folder
            #  structure (coverages, file geodatabase do)
            workspaces = set(arcpy.ListWorkspaces()) - \
                         set(arcpy.ListWorkspaces('', 'FILEGDB')) -\
                         set(arcpy.ListWorkspaces('', 'COVERAGE'))

            for workspace in workspaces:
                arcpy.env.workspace = os.path.join(root, workspace)
                fcs = arcpy.ListFeatureClasses(wild_card,

                if fcs:
                    list_fcs += [os.path.join(root, workspace, fc)
                                 for fc in fcs]

            for dataset in arcpy.ListDatasets('', 'FEATURE'):
                ds_fcs = arcpy.ListFeatureClasses(wild_card,
                if ds_fcs:
                    list_fcs += [os.path.join(
                        root, workspace, dataset, fc)
                                 for fc in ds_fcs]

    except Exception as err:
        raise err
        arcpy.env.workspace = preexisting_wks

    return list_fcs

6 thoughts on “Recursive list feature classes

    I think this script does not list feature classes within feature datasets within file geodatabases. That’s because the “walk” recurses into a fileGDB (as it’s a directory), lists the feature classes there, and then lists and processes workspaces, but feature datasets are not workspaces, so don’t get processed, and are not “recursed” into as they are not directories, and so neither they nor the feature classes they contain get recorded.

    Thursday 8th August 2013, 10:49 a.m. Australian Eastern Standard Time

  2. Yes, you’re correct. Thanks for catching that. The dataset handling part was indented too far in. I’ve update the code to address your comment.

  3. This script looks like someting very useful. How can we modifing this script to run in a local workspace for testing its fuctionality?

    • download the script.
      put paste it into a .py file
      add a line to call the recursive_list_fcs function, specify the first parameter to be your local workspace

  4. Hi
    Thanks for the useful script.
    I’m trying to do a print of the wildcard search in the script but it doesn’t return or print the list_fcs.
    I appreciate if you help with that.

    • i’m not sure what you mean, copy/paste the code into a .py file, add a line at the bottom like this

      for i in recursive_list_fcs(r"c:\temp"):

      then run the script from the windows terminal. You should get a long list of prints (if there’s geo data in c:\temp

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s