I have more than 300 layer files located in many sub-Folders and sub-sub-Folders. All Sub Folders are located in one large directory.I read Use python to add layers to TOC and I try, with arcpy, to detect all the layers and add them to mxd. Is it possible?

  • i use arcview 10.3 – newGIS Jan 19 '15 at 10:22

If you work with ArcGIS 10.1 SP1 or above, you can use the arcpy.da.Walk() function:

import arcpy, os

workspace = r"C:\directory"
mxd = arcpy.mapping.MapDocument(r"C:\Map.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)))

  • i get an error: Traceback (most recent call last): File "D:\desktop\python.py", line 13, in <module> arcpy.mapping.AddLayer(df, os.path.join(dirpath, filename)) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py", line 49, in AddLayer assert isinstance(add_layer, Layer) AssertionError – newGIS Jan 19 '15 at 12:17
  • There was an error in my script, sorry - I've updated it. – GISGe Jan 19 '15 at 12:21

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