ArcPy – List Feature Classes from Multiple Datasets in GDB

arcpyfeature-classfeature-datasetfile-geodatabaselist

I am trying to list all feature classes from multiple datasets with single GDB. I have tried with below code

import arcpy
from arcpy import env
env.workspace = r"C:\GDB\Base.gdb"
fdlist = arcpy.ListDatasets("*","feature")
for fd in fdlist:
    print fd
    
    env.workspace = fd
    fclist = arcpy.ListFeatureClasses()
    for fc in fclist:
        print(fc)

Lists a few feature classes in first dataset then display below error.I have found some scripts that works for single dataset.

Traceback (most recent call last):
File "C:\SOFTWARE\Arcpy\Arcpy_Script\List_fc.py", line 47, in
for fc in fclist:
TypeError: 'NoneType' object is not iterable

Best Answer

This will list all feature classes, inside a dataset and just sitting in the root of the geodatabase. It works with empty datasets. It works with an empty root. If you don't want to search the root, just leave the second part out.

import arcpy
from arcpy import env
env.workspace = r"C:\GDB\Base.gdb"

# list all FC in each dataset
fdlist = arcpy.ListDatasets("*","Feature")
for fd in fdlist:
    print("DS:", fd)
    fclist = arcpy.ListFeatureClasses("*","All",fd)
    for fc in fclist:
        print("\tFC:", fc)
    
# list all FC in root of GDB
fclist = arcpy.ListFeatureClasses()
for fc in fclist:
    print("FC:", fc)