List Feature Classes within Subfolders using Python

8419
5
Jump to solution
01-06-2011 07:16 AM
TomJansen
New Contributor
I'm trying to get a list of feature classes in a folder and it's subfolders.  Below is a script that gets just the folder specified.  Any help on how to loop through the subfolders is appreciated.


import arcpy
from arcpy import env
import os
# Set the workspace for the ListFeatureClass function
env.workspace = "C:/Data"
# Use the ListFeatureClasses function to return a list
fcList = arcpy.ListFeatureClasses()
print fcList
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
You could use a generator in this case:



import arcpy
import os

def fcs_in_workspace(workspace):
  arcpy.env.workspace = workspace
  for fc in arcpy.ListFeatureClasses():
    yield os.path.join(workspace, fc)
  for ws in arcpy.ListWorkspaces():
    for fc in fcs_in_workspace(os.path.join(workspace, ws)):
        yield fc

for fc in fcs_in_workspace("C:/Data"):
   ...


Or you could do something like
fclist = list(fcs_in_workspace("C:/Data"))
and reuse it.

View solution in original post

0 Kudos
5 Replies
JasonScheirer
Occasional Contributor III
import arcpy
import os

def fcs_in_workspace(workspace):
  arcpy.env.workspace = workspace
  for fc in arcpy.ListFeatureClasses():
    print os.path.join(workspace, fc)
  for ws in arcpy.ListWorkspaces():
    fcs_in_workspace(os.path.join(workspace, ws))

fcs_in_workspace("C:/Data")
ChristinaHerrick1
New Contributor
How would you then iterate through the list later in the script? 

Ex. "for fc in fclist: ..."
0 Kudos
JasonScheirer
Occasional Contributor III
You could use a generator in this case:



import arcpy
import os

def fcs_in_workspace(workspace):
  arcpy.env.workspace = workspace
  for fc in arcpy.ListFeatureClasses():
    yield os.path.join(workspace, fc)
  for ws in arcpy.ListWorkspaces():
    for fc in fcs_in_workspace(os.path.join(workspace, ws)):
        yield fc

for fc in fcs_in_workspace("C:/Data"):
   ...


Or you could do something like
fclist = list(fcs_in_workspace("C:/Data"))
and reuse it.
0 Kudos
ChristinaHerrick1
New Contributor
Awesome!  Thanks!
0 Kudos
JeffBigos
Esri Contributor

Hi All,

I wanted to add to additional ways to accomplish this and apply it to different data types.

I teach Python for Esri and have had this question come up in class alot.

1. Arcpy.da.walk:

     This was introduced to the data access module at 10.1 SP1

     ArcGIS Help (10.2, 10.2.1, and 10.2.2)

     Where this function is nice is in 2 parameters

     Datatype: the value set here is the filter for what is returned as walk traverses the subfolders

     type: not for all datasets, but at least you could specify point, line, poly or raster types

     Look at the Sample script 1 in the documentation

2. python os.walk:

    

     this function will perform the same type operation gathering subfolders and files

     The one difference is this won't retrieve contents inside of a geodatabase.

     This could be used if you script involved python code with out the use of arcpy

     maybe you needed to find all the .xls files under a D:\projects folder.

     15.1. os — Miscellaneous operating system interfaces — Python 2.7.8 documentation

Thanks,

Jeff