Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
1  
Are there also feature datasets within the geodatabases? –  blah238 Nov 2 '11 at 18:39
    
Nope, no feature datasets. –  Patty Jula Nov 2 '11 at 18:45
add comment

2 Answers

up vote 11 down vote accepted

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 list all feature classes in GDB, 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'

share|improve this answer
1  
Cool, that worked! I picked up a few other tools from what you shared too - didn't know I could use env.workspace like that. Thank you! –  Patty Jula Nov 2 '11 at 18:58
1  
@PattyJula You're welcome Patty. Also it's generally better to avoid generic words like 'item' as it could apply to anything and in larger scripts gets confusing. It's easier to keep in mind singular->plural distinction -- though you do need train yourself to watch for the trailing s. I use abbreviations to help with the latter. For example for wspace in workspaces: and for fc in featureclasses:. –  matt wilkie Nov 2 '11 at 19:10
add comment

You beat me to it! This script will, however, deal with any existing feature datasets (at least in theory - I haven't tested it). Same general idea though, if there are feature datasets, you need to set your working directory to that dataset before listing the features within.

import arcpy

dir = r'D:\output'
arcpy.env.workspace = dir

gdbList = arcpy.ListWorkspaces('*','FileGDB')

for gdb in gdbList:
    arcpy.env.workspace = gdb               #--change working directory to each GDB in list
    datasetList = arcpy.ListDatasets('*','Feature')     #--make a list of all (if any) feature datasets that exist in current GDB
    fcList = arcpy.ListFeatureClasses()         #--make a list of all feature in current GDB (root)
    for fc in fcList:
        print arcpy.env.workspace,fc            #--print directory,fc name
    for dataset in datasetList:
        arcpy.env.workspace = dataset   #--change working directory to each dataset (if any) in list
        fcList = arcpy.ListFeatureClasses()     #--make a list of all feature in current GDB (current dataset)
        for fc in fcList:
            print arcpy.env.workspace,fc        #--print directory,fc name
        arcpy.env.workspace = gdb
share|improve this answer
1  
+1 for comments and integrating FDs. I personally wouldn't use 'temp' as a variable name because of it's assocated meanings of "throw away" and "free to delete". The variable is discardable, but it's contents, the databases, are likely not. In any event, in this case it's unnecessary, just use arcpy.env.workspace = gdb as ListWorkspaces returns full path. ;-) –  matt wilkie Nov 2 '11 at 19:03
    
You're totally right, Matt. I will update the example for future reference. Also, I forgot a line at the end that resets the workspace to the parent GDB before trying to go to the next feature dataset (even though it's not necessary in this case). –  Jason Nov 2 '11 at 19:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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