Renaming FCs in a geodatabase using Python

290
2
Jump to solution
05-15-2023 07:57 PM
taka
by
New Contributor

 

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

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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))

 


Have a great day!
Johannes

View solution in original post

Tags (1)
2 Replies
JohannesLindner
MVP Frequent Contributor

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))

 


Have a great day!
Johannes
Tags (1)
taka
by
New Contributor

 

It's been a wonderful day. Thank you!

 

0 Kudos