2

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?

closed as off-topic by PolyGeo Jun 3 '18 at 10:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • i use arcview 10.3 – newGIS Jan 19 '15 at 10:22
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()
  • 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
1

This is possible with python. if this is part of your workflow in python you should do some python coding.
If your ultimate goal is to add the layers to the map (mxd) you can achieve this goal by:

1- go to the root folder of your layer files in the explorer

2- search for this term: *.lyr

3- use cntrl+A to select all lyr files.

4- drag them to the map of the ArcMap

As I mentioned, if this workflow is part of an automation in python you should tell and I will give some python code snippet.

  • thank's Farid,i tried to add layer files with python,GISGe answered me – newGIS Jan 26 '15 at 7:40

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