I am a beginner,
I'm not very good at English and I think it's hard to read.
I need a hint
I am new to Python but trying to rename hundreds of feature classes in a geodatabase.
This is the code I have been using that did not work so far:
import arcpy
import os
arcpy.env.workspace = r'C:\Users\WK48.TOKYO\Desktop\ooamisi9_r\01_gdb\ooamisi9.gdb'
lstDatasets = arcpy.ListDatasets()
for dataset in lstDatasets:
lstFCs = arcpy.ListFeatureClasses("*", "", dataset)
for fc in lstFCs:
oldName = str(fc)
newName = oldName.replace("_FG_GML", "")
newName = newName.replace("_0001", "")
arcpy.Rename_management(fc, newName)
I wrote the above code
No change in database
Solved! Go to Solution.
ListFeatureclasses() returns a list of fc names, but Rename expects complete paths. You have to construct the path from the workspace and the fc name.
import arcpy, os
all_fc_names = arcpy.ListTables() + arcpy.ListFeatureClasses()
for ds in arcpy.ListDatasets():
all_fc_names += arcpy.ListFeatureClasses(feature_dataset=ds)
for name in all_fc_names:
fc_path = os.path.join(arcpy.env.workspace, name)
new_name = name.replace("_FG_GML", "").replace("_0001", "")
try:
arcpy.management.Rename(old_path, new_name)
except Exception as e:
print("Could not rename " + name + ": " + str(e))
ListFeatureclasses() returns a list of fc names, but Rename expects complete paths. You have to construct the path from the workspace and the fc name.
import arcpy, os
all_fc_names = arcpy.ListTables() + arcpy.ListFeatureClasses()
for ds in arcpy.ListDatasets():
all_fc_names += arcpy.ListFeatureClasses(feature_dataset=ds)
for name in all_fc_names:
fc_path = os.path.join(arcpy.env.workspace, name)
new_name = name.replace("_FG_GML", "").replace("_0001", "")
try:
arcpy.management.Rename(old_path, new_name)
except Exception as e:
print("Could not rename " + name + ": " + str(e))
It's been a wonderful day. Thank you!