[GIS] Getting full path for feature classes in dataset using ArcPy

arcpy

I have a python script that I am writing to create a copy of a database and then clean it up (basically remove any empty items). Everything is working with the exception of feature classes that are embedded in a feature dataset. I have the portion of code that iterates through these items and is the source of my issue below:

arcpy.env.workspace = inputGDB
dataset_list= arcpy.ListDatasets()

for dataset in dataset_list: 
    featureclass_list=arcpy.ListFeatureClasses("","",dataset)
    for feature in featureclass_list:
        inpath=arcpy.Describe(feature).catalogPath

When I do this, the "catalogPath" returned is not the full path. It only contains the path up to the gdb and then appends the feature class name to it (e.g. it returns D:\Folders\Database.gdb\feature_name not D:\Folders\Database.gdb\Dataset\feature_name). The dataset which this feature was originally in is not returned with the above code. Now if I do:

item=arcpy.Describe(r"D:\Folders\Database.gdb\Dataset\feature_name")
print item.catalogPath

I get the correct path for the data in question. So the question I have is how do I get the full data path for feature classes embedded in a dataset with some option similar the the first snippet of code?

Best Answer

Try something like this instead:

arcpy.env.workspace = inputGDB
dataset_list = arcpy.ListDatasets()

for dataset in dataset_list: 
    for child in arcpy.Describe(dataset).children:
        if hasattr(child, 'datasetType') and child.datasetType == 'FeatureClass':
            inpath = child.catalogPath

It seems like a bug that catalogPath is different depending on how you obtain the Describe object reference.

See also: Creating table containing all filenames (and possibly metadata) in File Geodatabase?