I have produced a Python script which creates multiple buffers of specific (listed) feature classes within a geodatabase. This is achieved by running each feature class through a for loop.

I would like to know if/how it would be possible to list feature classes from more than one geodatabase?

Currently I am using env.workspace to specify the single geodatabase. Listing the feature classes and then running them through the 'for loop'. A possible problem might be that many of the feature classes in the various geodatabases have the same name.

import arcpy
import os.path
import tempfile
from arcpy import env

# sets the workspace to my gdb
arcpy.env.workspace = "C:/GIS Home/Mining Features (MATTHEW).gdb"

# Output workspace and temp location is defined
output_workspace = "C:/GIS Home/project_1"
temp_workspace = tempfile.mkdtemp()

# list of features to buffer in gdb
fcList = ["Adit_Metalliferous", "Shaft_Metalliferous", "Quarry_Metalliferous", "Dump_Metalliferous", "Engine_House", "Goffan_Working", "Lode_Surface_Outcrop", "Lode_Undefined_Elevation", "Mine_Level", "Mine_Raise", "Portal", "Reported_Subsidence", "Secured_Feature", "Tunnel"]

#create empty lists for output
l_250 = []
l_500 = []
l_750 = []

for featureClass in fcList:
    b250 = os.path.join(temp_workspace, featureClass + "_b250.shp")  # make temp output file
    b500 = os.path.join(temp_workspace, featureClass + "_b500.shp")  # make temp output file
    b750 = os.path.join(temp_workspace, featureClass + "_b750.shp")  # make temp output file
    arcpy.Buffer_analysis(featureClass, b250, 250, "", "", "ALL")   # carry out buffer
    arcpy.Buffer_analysis(featureClass, b500, 500, "", "", "ALL")   # carry out buffer
    arcpy.Buffer_analysis(featureClass, b750, 750, "", "", "ALL")   # carry out buffer
    l_250.append(b250)  # append the buffer to the l_250 list
    l_500.append(b500)  # append the buffer to the l_500 list
    l_750.append(b750)  # append the buffer to the l_750 list

merge_250 = os.path.join(temp_workspace, "250_merge.shp")
arcpy.Merge_management(l_250, merge_250)
merge_500 = os.path.join(temp_workspace, "500_merge.shp")
arcpy.Merge_management(l_500, merge_500)
merge_750 = os.path.join(temp_workspace, "750_merge.shp")
arcpy.Merge_management(l_750, merge_750)

dissolve_250 = os.path.join(output_workspace, "250_dissolve.shp") # create 250_dissolve.shp for final output
arcpy.Dissolve_management(merge_250, dissolve_250)
dissolve_500 = os.path.join(output_workspace, "500_dissolve.shp") # create 500_dissolve.shp for final output
arcpy.Dissolve_management(merge_500, dissolve_500)
dissolve_750 = os.path.join(output_workspace, "750_dissolve.shp") # create 750_dissolve.shp for final output
arcpy.Dissolve_management(merge_750, dissolve_750)
  • 2
    You could create a python list with the pathname for each gdb (workspaceList = ["C:/xxx", "C:/yyy", "C:/zzz"]). When you loop through the features within a gdb you could add an identifier to the filename that references the current workspace.
    – Roy
    Commented Oct 2, 2012 at 20:11

2 Answers 2


To read from different sources with a 'wildcard (*)' use glob

import glob

gdbs = glob.glob("D:\\geodata\*\*.gdb") # looks for all .gdb's in sub- folders of geodata
for gdb in gdbs:
    # do stuff...

To get the names of the features in the gdb you better define a function based on this answer:

def feat_in_gdb(name):
    return [[os.path.join(feat, feat_class) for feat_class in \
    gp.ListFeatureClasses('','',feat)] \
    for feat in gp.ListDatasets('','feature')]

I'd also define functions for the geoprocessing as you call those methods so often, and will do this even more so when you alter your loop.


There are several ways to do this. If you want to get all the .gdbs, then LarsVegas' answer is the way to go. If you have only specific gdbs you want to target, then you could do this:

import arcpy
import os

gdb_dir = r"D:\Projects\GDBs"

gdbs = ["scratch.gdb", "slowbutter.gdb"]

for gdb in gdbs:
    arcpy.env.workspace = os.path.join(gdb_dir, gdb)
    print arcpy.env.workspace
    fc_list = [...]
    for fc in fc_list:
        # do whatever

You could take it a step further and have a list of gdb_dirs if your geodatabases are in different directories, and then iterate through those as well. You're essentially taking the different pieces of the file paths, breaking them up, them looping through each one and piecing them back together as you go.

Based on your comment below, try this (untested):

gdb_dirs = ["C:/GIS/dir1", "C:/GIS/dir2", "C:/GIS/dir3"]
gdbs = {"geo1.gdb":["list of feature classes"],
        "geo2.gdb":["list of feature classes"],
        "geo3.gdb":["list of feature classes"]}
for gdb_dir in gdb_dirs:
    for gdb, fc_list in gdbs.iteritems():
        arcpy.env.workspace = os.path.join(gdb_dir, gdb)
        for fc in fc_list: 
            DO STUFF
  • Thanks Chad Cooper and @LarsVegas. Would the following work ok?
    – Matt
    Commented Oct 3, 2012 at 15:49
  • gdb_dir = [“C:/GIS/dir1”, “C:/GIS/dir2”, “C:/GIS/dir3”] for gdb in gdb_dir: arcpy.env.workspace = os.path.join(gdb_dir, gdb) if gdb == "geo1.gdb": fcList = ["list of feature classes"] for featureClass in fcList: DO STUFF elif gdb == "geo2.gdb" fcList = ["list of feature classes”] for featureClass in fcList: DO STUFF elif gdb == "geo3.gdb" fcList = list of feature classes"] for featureClass in fcList: DO STUFF
    – Matt
    Commented Oct 3, 2012 at 15:50
  • @MattWalsh see my edit above. Commented Oct 3, 2012 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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