Sunday 2 February 2020

arcgis 10.0 - Listing feature classes in multiple geodatabases in folder using ArcPy?


I have a folder with numerous geodatabases in it. Within the geodatabases are feature classes. I would like to get a list of the feature classes in these geodatabases using a Python script. I've only gotten so far as listing the geodatabases. Does anyone have a suggestion on listing the feature classes in the geodatabases?



The print item line in my script gives me the following results:


C:\output\data.gdb 
C:\output\otherdata.gdb
C:\output\somethingelse.gdb

I want to generate a list of the feature classes in the geodatabases above. Below is my script so far.


    import arcpy, os, sys
from arcpy import env

arcpy.env.workspace = "D:\\output"

inWorkspace = arcpy.env.workspace

workspaces = arcpy.ListWorkspaces("*", "FileGDB")
for item in workspaces:
print item #This part gives me the print statements I shared above
# fcList = arcpy.ListFeatureClasses() #I haven't figured this part out
#I want to list the feature classes in the geodatabases

Answer



The trick you're missing is making each gdb the active workspace before listing the contents:


for item in workspaces:

print item
env.workspace = item
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
print '\t', fc

Also note that this will miss an feature classes inside feature datasets, see Listing all feature classes in File Geodatabase, including within feature datasets? to solve that.


More generally, if you use r you don't need to double backslash everything (makes for easier copy and paste from windows explorer address bar etc.): e.g. r'D:\output'


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...