Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From python how can I build a list of all feature classes in a file geodatabase (*.gdb), including inside feature datasets? The standard example only lists feature classes at the top level of the geodatabase:

import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.workspace = 'd:\scratch.gdb'
fcs = gp.ListFeatureClasses()

for fc in fcs:
    print fc

Please indicate which ArcGIS Desktop version your answer applies to (I am looking for 9.3 but we might as well collect all versions in one place).

share|improve this question
A related but more up to date Q&A (including use of arcpy.da.Walk) is How to make a GIS inventory? – blah238 May 22 '14 at 19:30

This routine for arcgis10 returns all fcs (standalone OR within a feature dataset) inside a gdb. Just set your arcpy.env.workspace then do a for loop

def listFcsInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(arcpy.env.workspace, fds, fc)
share|improve this answer
Nice and clean! – Chad Cooper Feb 8 '11 at 19:10
thanks gotchula! That's the first time I've encountered the yield statement, had to do a bit of reading to figure it out. You forgot to note your sample is for arcgis v10. – matt wilkie Feb 8 '11 at 22:28
sorry, yeah this is for 10.x. and yes, the yield is great, makes for clean code. – gotchula Feb 8 '11 at 23:09
up vote 8 down vote accepted

I ended up using gotchula's answer, but without yield because I generally re-use the FC handles created and yield's are used once then discarded it's easier for me to read and understand what fcs.append() is doing than fcs = yield(...).

def listFcsInGDB(gdb):
    ''' list all Feature Classes in a geodatabase, including inside Feature Datasets '''
    arcpy.env.workspace = gdb
    print 'Processing ', arcpy.env.workspace

    fcs = []
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            #yield os.path.join(fds, fc)
            fcs.append(os.path.join(fds, fc))
    return fcs

gdb = sys.argv [1]
fcs = listFcsInGDB(gdb)
for fc in fcs:
    print fc            


d:\> python r:\v5\YT_Canvec.gdb
Processing  r:\v5\YT_Canvec.gdb

This is now in a module I call arcplus*. Place with your other code or PYTHONPATH and then:

import arcplus
fcs = arcplus.listAllFeatureClasses('d:\default.gdb')
for fc in fcs:
    print "magic happens with: ", fc

Arcplus also adds wildcard filtering; to process only feature classes that start with "HD_" within feature datasets containing "Hydro"

fcs = arcplus.listAllFeatureClasses(gdb, fd_filter='*Hydro*', fc_filter='HD_*')

.* now on Github, upgraded for 10.x. For arcgis 9.3 see here.

share|improve this answer
Not sure I understand the rationale behind avoiding use of yield here. For one, the "handles" you refer to are not handles at all, they're just strings. And if your intention is to keep the list of feature classes around for multiple iterations, you can still keep it as a generator function and just "listify" it: my_list = list(generator_function(args)) this evaluates the generator and stores the result in a list variable. – blah238 May 22 '14 at 19:39
@blah238: oh. I guess I still don't grok yield then. I understand what a statement like fcs = fcs.append(...) is doing much more quickly than fcs = list(yield(...)). – matt wilkie May 26 '14 at 18:07

The ListDatasets method is what I think you are looking for. This FGDB has a FD in it called "Wells" and it has 3 FCs in it. For 9.3.1:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcgisscripting
>>> gp=arcgisscripting.create(9.3)
>>> d='c:\data\Third_Party_Wells_PRD.gdb'
>>> gp.workspace = d
>>> fds=gp.ListDatasets('','Feature')
>>> for fd in fds:
...     print fd
>>> for fd in fds:
...     gp.workspace=d + '/' + fd
...     fcs=gp.ListFeatureClasses()
...     for fc in fcs:
...             print fc
share|improve this answer

Your Answer


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.