I have 22 geodatabases all in one folder. Each gdb has two feature classes. For each feature class, I need to rename a few of the fields. I've had success with this code, but it only works for each GDB at a time:

import arcpy
from arcpy import env
env.workspace = r'C:\...\filename.GDB'

for fc in arcpy.ListFeatureClasses('*'):
        arcpy.AlterField_management(fc, 'ADDR_SN', 'STREET_NAME', 'STREET_NAME')

As I said, that code is just for one GDB at a time, so I'd have to change the name in env.workspace each time. That's not a big deal, I just want to see if it's possible to iterate through every GDB in the folder. So I tried adding into the code:

for workspace in arcpy.ListWorkspaces('*', 'All'):

When I add the above code, I also change the env.workspace to the folder name that contains all the geodatabases, but nothing happens. No error is returned, nothing happens.

  • 1
    Welcome to GIS SE! As a new user be sure to take the Tour where you will see that only one question should be asked per question. I recommend that you use the edit button beneath your question to edit the second one out. Also, you have posted some code and then mentioned that you edited it but what we really need to see is precisely what you ran when you were stuck and any errors thrown. While you are testing and want to see all errors I suggest removing your try/except. – PolyGeo Mar 25 '15 at 22:55
  • Ok sorry about that. I figured out the array thing on my own. I still would like to know how to iterate through all GDB though. Here's the working array code: import arcpy from arcpy import env env.workspace = r'C:\....\filename.GDB' ... oldfields = ['old_name1', ..., 'old_nameN'] ... newfields = ['new_name1, ..., 'new_nameN'] for fc in arcpy.ListFeatureClasses('*'): ... for i in range(len(oldfields)): try: arcpy.AlterField_management(fc, oldfields[i], newfields[i], newfields[i]) except: pass – Krewe Chief Mar 26 '15 at 13:17
  • 1
    Would you be able to specify the version of ArcGIS for Desktop that you are using, please? For the question you are now asking it is likely to make a big difference. – PolyGeo Mar 26 '15 at 19:49
  • Yes I'm using 10.3. I'm doing this in ArcCatalog, but I doubt that makes a difference. – Krewe Chief Mar 26 '15 at 19:56

So ESRI has arcpy versions of some common operations from the os module, especially when it comes to listing directories. I have not used arcpy.ListWorkspaces() before, but it seems like it should work for you, and you could add arcpy.ListWorkspaces("*","FileGDB") because you are specifically looking for File Geodatabases. Generally it's good practice to use arcpy functions when dealing with ESRI data, but in some cases like this it is not necessary.

EDIT: As @polygeo mentioned below, the best thing to use is arcpy.da.walk() if you're using ArcGIS 10.1+. Here's more information. The rest of my answer below uses the os.walk() function: workable, but not the best.

The os module alternative would be to use os.walk. This is a super useful function for everywhere so it's good to be familiar with it. Here's documentation.

For your use, it could be something like this:

import os
import arcpy
from arcpy import env

topdir = r"path/to/folder/containing/all/gdbs"

for path, dirs, files in os.walk(topdir):

    # iterate through all the directories
    for d in dirs:

        #skip directories that are not geodatabases
        if not d.lower().endswith(".gdb"):

        # now you know you are looking at a directory that is a gdb
        env.workspace = os.path.join(path,d)
        print env.workspace #just to double check

        for fc in arcpy.ListFeatureClasses():

Note: Once you are "in" a file geodatabase, the os module is not useful, because although the geodatabase can (more or less) be treated as a normal directory, the individual files inside of it are not recognizable as spatial data unless you access them with the arcpy module. For example, if you have a feature class called "roads", os.path.isfile(r"c:\geodatabase.gdb\roads") == False because no file actually exists called roads.

|improve this answer|||||
  • 3
    Rather than os.walk I recommend arcpy.da.Walk because it is able to walk inside geodatabases. – PolyGeo Mar 26 '15 at 19:59
  • aha, yes, I'd agree, thanks. I forgot about that function. – mr.adam Mar 26 '15 at 20:35

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.