Select to view content in your preferred language

Need some Python to add a suffix of _OLD to all features in enterprise geodatabase

429
1
09-26-2024 03:44 AM
AndrewReynoldsDevon
Regular Contributor

I need to rename up to 100 features which are a mixture of tables & poly's & lines to add the suffix _OLD, & then _NEW & then to remove the suffix altogether as part of renaming of features when importing new data.

I've had a go at some code but get the attached error - any ideas?

import arcpy

# Set the workspace to your enterprise geodatabase
arcpy.env.workspace = "Path to SDE"

# List all feature classes and tables in the geodatabase
datasets = arcpy.ListFeatureClasses() + arcpy.ListTables()

# Loop through each dataset and rename it
for dataset in datasets:
    new_name = f"{dataset}_OLD"
    arcpy.management.Rename(dataset, new_name)

print("All items have been renamed with the suffix '_OLD'.")
Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 7253, in Rename
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 7250, in Rename
    retval = convertArcObjectToPythonObject(gp.Rename_management(*gp_fixargs((in_data, out_data, data_type), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 520, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 160068: The dataset name is invalid.
Failed to execute (Rename).

 

Tags (1)
0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor

@AndrewReynoldsDevon I would add a print statement to see which feature class it's failing on:

# Loop through each dataset and rename it
for dataset in datasets:
    print(f"Renaming {dataset}")
    new_name = f"{dataset}_OLD"
    arcpy.management.Rename(dataset, new_name)