Select to view content in your preferred language

arcpy.ListFeatureClasses() Problem

6003
7
04-23-2013 09:03 AM
MichaelVolz
Esteemed Contributor
To All Python Users:

I have a python script with arcpy.ListFeatureClasses() that I wanted to use to get a list of all feature classes in a file geodatabase.  This method has worked perfectly in the past, but I have encountered a scenario where it does not work.

In this scenario I have a file geodatabase with a feature dataset that has the same name as a feature class.  When I run the python script it does not include this feature class in the list.  If I change the name of the feature class, the python script adds the feature class name to the list.  This is not my data, so I cannot change the name of the feature class in production.

Has anyone encountered this issue with arcpy.ListFeatureClasses()?

Any help or feedback is greatly appreciated.  Thank you.
Tags (2)
0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Michael,

After a quick test, I was not able to reproduce this. 

1.  What version of ArcGIS (including service packs) are you running? 

2.  What version is the file geodatabase?

3.  Can you reproduce this on other file geodatabases?
0 Kudos
ChrisSnyder
Honored Contributor
You are setting the workspace to your feature dataset 1st (in order to list the FCs there) right?

If you have v10.1 SP1, I have found the new arcpy.da.walk function to be very handy: http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000
0 Kudos
RDHarles
Regular Contributor
This is how I've always done it...

# For each ws, print the fc's and tbl's.
for ws in arcpy.ListWorkspaces("*", "FileGDB"):    
    arcpy.env.workspace = ws
    # feature classes
    for fc in arcpy.ListFeatureClasses():    
        print fc       
    # tables
    for tbl in arcpy.ListTables():
        print tbl       
    # feature classes in datasets
    for ds in arcpy.ListDatasets():
        arcpy.env.workspace = ds
        for dfc in arcpy.ListFeatureClasses():    
            print dfc           
0 Kudos
RDHarles
Regular Contributor

If you have v10.1 SP1, I have found the new arcpy.da.walk function to be very handy: http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000



How's it going, Chris!
Haven't played with the arcpy.da stuff.  Looks like it can replace my old system walk stuff.

import os, subprocess


for root, dirs, files in os.walk(os.getcwd()):
    for dir in dirs:        
        if dir.endswith(".gdb"):
            fullpath = os.path.abspath(os.path.join(root, dir))
            print "\nfullpath: "+fullpath
            print "dir: "+dir
0 Kudos
MichaelVolz
Esteemed Contributor
Thank you R.D. Harles and Chris

I thought I could get a list of all the feature classes in the file geodatabase, irregardless of whether they were in a feature dataset, from the ListFeatureClasses method, but I was incorrect.  This is very useful information.
0 Kudos
MichaelVolz
Esteemed Contributor
I have taken the following code and added it to my scripts to cycle through the Feature Datasets.

for ds in arcpy.ListDatasets():
        arcpy.env.workspace = ds
        for dfc in arcpy.ListFeatureClasses():   
            print dfc

Unfortunately, when I make the call to for dfc in arcpy.ListFeatureClasses(): in the loop for the 2nd dataset, the python script exits without any error message.

Would anyone know what I am missing for the script to execute properly against the 2nd Feature Dataset in the file geodatabase?
0 Kudos
RDHarles
Regular Contributor
Alright, that is really bizarre.  You're right, that code will only loop through one dataset?!?!
The data I originally tested it on only had one dataset so I never noticed.
Not sure if that's a bug or what but it doesn't work in 10.0 or 10.1!

But, if you do it like this, it will work...

import arcpy, os


arcpy.env.workspace = os.getcwd()


# For each ws, print the fc's and tbl's.
for ws in arcpy.ListWorkspaces("*", "FileGDB"):    
    arcpy.env.workspace = ws
    # feature classes
    for fc in arcpy.ListFeatureClasses():    
        print "fc: "+fc       
    # tables
    for tbl in arcpy.ListTables():
        print "tbl: "+tbl       
    # feature classes in datasets
    for ds in arcpy.ListDatasets():
        print "ds: "+ds       
        for dfc in arcpy.ListFeatureClasses("*", "All", ds):    
            print "fc in ds: "+dfc
0 Kudos