1

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

3
  • 1
    First off, the only feature classes that should be in a feature dataset are the ones that need to be in one, so you should have plenty of FCs outside any FDS, which this code won't detect. It also doesn't handle empty FDS -- you might want to try for fc in fclist if fclist else []:
    – Vince
    Dec 1, 2022 at 2:32
  • Hi, neither feature classes outside GDB nor empty FDs. There are totally 15 datasets in my GDB.Above code printing feature classes that are in first dataset and then getting an TypeError: 'NoneType' object is not iterable
    – Arb
    Dec 1, 2022 at 2:57
  • 1
    There's only two places in that code that can give that error. Go ahead and print the stringified value of each list before using them
    – Vince
    Dec 1, 2022 at 3:11

3 Answers 3

1

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)
1
  • 1
    Thank you. Now it's working when I remove the line env.workspace = fd. I understood that this works if there are multiple GDBs.
    – Arb
    Dec 18, 2022 at 3:48
1

You can limit the ListFeatureClasses to a feature dataset eg arcpy.ListFeatureClasses(feature_dataset=fd).

Not sure if the env.workspace=fd is working maybe it only works to the gdb level. See https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/listfeatureclasses.htm

0
1

Listing and finding data sets is best accomplished using Walk - ArcGIS Pro | Documentation. ArcPy DA Walk mimics the behavior of Python's os.walk, and doesn't rely on setting workspace environments, which is both slow and prone to mistakes.

It appears this question is about listing all the feature classes in a geodatabase, both stand-alone and those in datasets. This is easily accomplished with ArcPy DA Walk:

import arcpy
import os

# Set geodatabase
input_gdb = # path to geodatabase

# Get list of polygon feature classes in GDB
walk = arcpy.da.Walk(input_gdb, datatype="FeatureClass")
fc_list = [
    os.path.join(root, feature_class)
    for root, data_sets, feature_classes in walk
    for feature_class in feature_classes
]

print(*fc_list, sep="\n")
1
  • That’s great! I didn’t know about this walk ability. I would add that while this tool doesn’t depend on workspace, many tools do, so depending on what you plan on doing with the feature classes you might need to set it anyway. Regardless, love this tool!
    – alexGIS
    Dec 19, 2022 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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