Rename feature classes iteratively through multiple feature datasets

3582
1
12-04-2014 05:21 PM
GavanThomas
New Contributor II

I have a file geodatabase (FGDB) with many feature datasets (FDS) whose names I want to use to rename their constituent feature classes in a prefix-like manner, for example:

 

FDS_rename.jpg

The first FDS shows how its name is used as the prefix for all the constituent features, whereas the second FDS has feature classes that need to be renamed. You'll notice there are 3 numerals appended to each feature class that need to be trimmed off as well.

 

I have found several limitations with arcpy in ArcGIS 10.1 (SP1) in that tools like ListFeatureClasses and ListDatasets don't like iterating through multiple FDSs in the same FGDB. I am wondering if the solution is in the use of Iterators in the ModelBuilder environment? Has anyone out there had any similar experiences and success in processing a large collection of FDSs?

 

Thanks in advance

 

Gavan

0 Kudos
1 Reply
JoshuaBixby
MVP Esteemed Contributor

The functional code for recursively listing feature classes in a geodatabase is provided as ListFeatureClasses example 1 in the help for ListFeatureClasses (arcpy).  The topic was also covered in the Recursive list feature classes post at ArcPy Cafe a couple years back.

For your situation, I believe the following code will work:

import os
ws = #path to gdb
arcpy.env.workspace =  ws
for ds in arcpy.ListDatasets(feature_type="Feature"):
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        arcpy.Rename_management(
            os.path.join(ws, ds, fc),
            os.path.join(ws, ds, '{}_{}'.format(ds,fc[:-3]))
        )

The code above will rename what you have already manually renamed.

0 Kudos