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?
Asked
Active
Viewed 1,881 times
2
-
i use arcview 10.3 – newGIS Jan 19 '15 at 10:22
1 Answers
4
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)))
mxd.save()
GISGe
- 9,674
- 19
- 41
-
i get an error: Traceback (most recent call last): File "D:\desktop\python.py", line 13, in
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 -