Rename feature class using arcpy python

3550
3
09-09-2019 05:39 AM
AnandRao
Occasional Contributor

Hello,

There are multiple FileGDB in a folder. Each of these FIleGDB contain multiple feature classes. I want rename the feature class name in such a way that the name of respective FileGDB is added to its name

For instance

FileGDB name is Bus.gdb. Feature classes with in this gdb are Stops and Routes. I want to rename these feature classes as Bus_Stops and Bus_Routes.

Ii would like to use arcpy to achieve this

Thank you

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

It is normally done  with

Rename—Data Management toolbox | ArcGIS Desktop 

You will need to list your featureclasses and parse on the required gdb name to each one.

The code example shows the basic syntax for one

JohannesBierer
Occasional Contributor III

Maybe you can try this, you have to change the path:

import os
import arcpy

top_folder = r"your path"

for path, dirs, files in os.walk(top_folder):
    for d in dirs:
        if not d.endswith(".gdb"):
            continue
        gdb_path = os.path.join(path, d)
        
        arcpy.env.workspace = gdb_path
        
        featureclasses = arcpy.ListFeatureClasses()
        
        for fc in featureclasses:
      
            arcpy.Rename_management(fc, "{}_{}".format(d[:-4], fc))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
AnandRao
Occasional Contributor

Thank you Johannes for your reply.It is helpful.

Actually,I tried the below code and was able to get the expected results

import arcpy
arcpy.env.workspace = r"my_workspace"
wks = arcpy.ListWorkspaces('*','FileGDB')
for fgdb in wks:
    arcpy.env.workspace = fgdb
    fClasses = arcpy.ListFeatureClasses('*','','Placemarks')
    for fc in fClasses:
    arcpy.Rename_management(fc,fgdb.split('.')[0]+"_"+fc ,'')

0 Kudos