I have GDB files located in many sub-Folders and sub-sub-Folders. All Sub Folders are located in one large directory i asked it for layer file in Add multiple Layer files in subdirectories to mxd using python I use this code:

import arcpy  
import os  

workspace = r"C:\Project\layers"
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
walk = arcpy.da.Walk(workspace, datatype="Layer")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(dirpath, filename)))
        print filename
del mxd

How can i do it for GDB files ?

  • It's not possible to add a GDB to an mxd... with arcpy you can't even add a feature class. All you can add is a layer. What do you exactly want to do? See this question for a possible option. – GISGe Jan 20 '15 at 12:48
  • 1
    @GISGe I agree that it makes no sense to try and add a *.gdb to a map but this Q&A shows how you can add a feature class (shapefile) as a layer: gis.stackexchange.com/questions/4882/… – PolyGeo Jan 20 '15 at 13:01
  • @PolyGeo that's true indeed! So the documentation is wrong. Those Layer functions are less restrictive than they should then. Thanks for pointing to this. – GISGe Jan 20 '15 at 13:14

So based on @PolyGeo's comment you should just replace the datatype parameter in the arcpy.da.Walk() function:

walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

The rest of the code can be reused as it is.

| improve this answer | |
  • the code add all layers in the folder and not just the gdb files – newGIS Jan 26 '15 at 10:50

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.