1

I am new to Python but trying to rename hundreds of feature classes in a geodatabase. For instance, I have over 50 FCs named 'LAND_*'.

Can I with my Python script to run in ArcGIS Pro to rename the 'LAND' part of the file to 'INFRA'? Instead of 'LAND_A' you would rename to 'INFRA_A'?

I know how to do this individually but not how to do for all the feature classes in a geodatabase. I have looked through many examples but have not yet found one that works.

This is the code I have been using that did not work so far:

arcpy.env.workspace = "C:\\Temp\\Test.gdb"
for dataset in lstDatasets:
    lstFCs = arcpy.ListFeatureClasses("LAND_*", "", dataset)
    for fc in lstFCs:
        oldName = str(fc)
        newName = oldName.replace("LAND", "INFRA")
        arcpy.Rename_management(fc,newName)

I am getting the following error when I try to run the code:

"Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'lstDatasets' is not defined"

I was able to get to the exact solution I needed by using the following script:

arcpy.env.workspace = "C:\\Temp\\test_GDB.gdb"
lstDatasets = arcpy.ListDatasets()
for dataset in lstDatasets:
    print (dataset)
lstFCs = arcpy.ListFeatureClasses("LAND_*")
for fc in lstFCs:
    print (fc)
    oldName = str(fc)
    newName = oldName.replace("LAND_", "INFRA_")
    arcpy.Rename_management(fc,newName)
  • You can use for InFCname in ListFeatureClasses('LAND_') to iterate after setting arcpy.env.workspace to your geodatabase then use arcpy.Rename_management(InFCname,InFCname.replace('LAND','INFRA')), to be any more specific and to avoid any potential problems I would need to see your code. Beware python is case sensitive so if your feature class is called Land_.. the replace statement *will not replace Land with INFRA. – Michael Stimson Oct 26 '18 at 0:13
  • Hi, Thanks for your reply - as I said I'm very new to python - but here is one of the codes that failed:nv.workspace = 'gdb filepath' for dataset in lstDatasets: lstFCs = arcpy.ListFeatureClasses("LAND_*", "", dataset) for fc in lstFCs: oldName = str(fc) newName = oldName.replace("LAND", "INFRA") arcpy.Rename_management(fc,newName) – Kerry1A Oct 26 '18 at 0:18
  • What error are you getting? I would suggest instead of doing a replace, which is case sensitive, use newName = 'INFRA' + fc[4:] to generate the new name. Then change your workspace to os.path.join( 'gdb filepath',dataset) before attempting to rename. – Michael Stimson Oct 26 '18 at 0:34
  • You should post your solution as an answer, not as an edit to the question. Please give thanks to those who helped you by voting on their answers – nmtoken Oct 31 '18 at 9:30
1

Unless you didn't post all your code, it looks like lstDatasets is never assigned any value. This would explain the error name 'lstDatasets' is not defined.

If you want to rename featureclasses that are inside a featuredataset, you should loop over those, and for each featuredataset, loop over the featureclasses. E.g.

arcpy.env.workspace = r"C:\Temp\Test.gdb"

lstDatasets = arcpy.ListDatasets()

for dataset in lstDatasets:
    print (dataset)
    lstFCs = arcpy.ListFeatureClasses("LAND_*", None, dataset)
    for fc in lstFCs:
        print (fc)
        oldName = str(fc)
        newName = oldName.replace("LAND_", "INFRA_")
        arcpy.Rename_management(fc,newName)

If on the other hand you have featureclasses that are not in a featuredataset, you can just leave out the outer loop:

lstFCs = arcpy.ListFeatureClasses("LAND_*")
for fc in lstFCs:
    print (fc)
    oldName = str(fc)
    newName = oldName.replace("LAND_", "INFRA_")
    arcpy.Rename_management(fc,newName)

If you don't know what a featuredataset is, read this.

0

it looks like the error is in lstDatasets ... try adding in a print line to see what it is throwing back at you

for dataset in lstDatasets:
        ##
    print dataset 
        ##
    lstFCs = arcpy.ListFeatureClasses("LAND_*", "", dataset) 
        for fc in lstFCs: 
            oldName = str(fc) 
            newName = oldName.replace("LAND", "INFRA")
            arcpy.Rename_management(fc,newName)
  • Thank you - I tried that code and got this message: File "<string>", line 3 print dataset ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(dataset)? – Kerry1A Oct 26 '18 at 2:06
  • yes, that is what you want...if running in python 3 you cant print "bla" anymore. You have to print ("blah") – Sorin RUSU Oct 26 '18 at 7:40
  • Thanks Alex S1 and everyone else who contributed. I finally got this to work and it is exactly what was required: I will edit the question to show this. – Kerry1A Oct 30 '18 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.