Select to view content in your preferred language

Renaming features within feature dataset giving unexpected result

2034
2
03-23-2012 10:48 AM
HenryUnderwood
Emerging Contributor
Hi All,

A portion of my script goes into a geodatabase, deletes some features (ending in _old), and renames some features (adds _old to existing). It's part of a larger script that will replace, rename, and delete various feature classes in a file geodatabase. I get an unexpected result: one of my features has the same name as the container dataset. This results in the dataset being renamed instead of the feature. How can I avoid this? Does my script make sense?

I have added some before and after shots to show what is happening.

[ATTACH=CONFIG]12969[/ATTACH][ATTACH=CONFIG]12970[/ATTACH]

fdss = arcpy.ListDatasets()

for fds in fdss:
    print fds
    if fds == "Streets":
        fcs = arcpy.ListFeatureClasses("", "", "Streets")
        for fc in fcs:
             if fc == "streets_old":
                 print "Deleting old streets"
                 arcpy.Delete_management(fc, "Feature Class")
             elif fc == "arterials_old":
                 print "Deleting old arterials"
                 arcpy.Delete_management(fc, "Feature Class")
             elif fc == "streets":
                 arcpy.Rename_management(fc, fc + "_old", "Feature Class")
                 print "Renaming current streets to old"
             elif fc == "arterials":
                 arcpy.Rename_management(fc, fc + "_old", "Feature Class")
                 print "Renaming current arterials to old"
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: mdenil

You likely want to use ListFeatureClasses instead of ListDatasets
OR
test the Dataset properties with Describe before performing the operation. The datasetType property will tell you if it is a FeatureDataset or something else.
0 Kudos
HenryUnderwood
Emerging Contributor
"Streets" is a feature dataset, "streets" is a feature class. I honestly don't understand why listfeatureclasses has "streets" as desc.dataType "FeatureDataset". One would expect "FeatureClass".

That's when I found the error. And I do owe it to printing out the data types. Thank you for that suggestion. Under rename_Management I had "Feature Class" instead of "FeatureClass".

Working code:
fcs = arcpy.ListFeatureClasses("","", "Streets")
for fc in fcs:
     desc = arcpy.Describe(fc)
     if desc.baseName == "streets_old":
         print desc.dataType
         print "Deleting old streets"
         arcpy.Delete_management(fc)
     elif desc.baseName == "arterials_old":
         print desc.dataType
         print "Deleting old arterials"
         arcpy.Delete_management(fc)
     elif desc.baseName == "streets":
         print desc.dataType
         arcpy.Rename_management(fc, fc + "_old", "FeatureClass")
         print "Renaming current streets to old"
     elif desc.baseName == "arterials":
         print desc.dataType
         arcpy.Rename_management(fc, fc + "_old", "FeatureClass")
         print "Renaming current arterials to old"



0 Kudos