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 Answers 3
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)
-
1Thank you. Now it's working when I remove the line env.workspace = fd. I understood that this works if there are multiple GDBs.Arb– Arb2022年12月18日 03:48:57 +00:00Commented Dec 18, 2022 at 3:48
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
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")
-
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– alexGIS2022年12月19日 05:50:13 +00:00Commented Dec 19, 2022 at 5:50
Explore related questions
See similar questions with these tags.
for fc in fclist if fclist else []: