1

I tried to add several layer files to MXD file. when i run this code no error rise- but only one layer file added to the MXD and all the other 34 layer files in the workspace aren't added to the map. This is the table of content after runnig the code:

enter image description here

import arcpy,os
workspace = r"G:\desktop\Project\lyr\taba7650円"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
for dirpath, dirnames, filenames in walk:
 for filename in filenames:
 arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(dirpath, filename)))
mxd.save()

How can i add the layers to the Mxd?

i red

asked Jun 3, 2018 at 10:17
2
  • 1
    Try running the code snippet that you have presented. I think it will error first when it hits no new line after the colon in filenames:arcpy.mapping. Commented Jun 3, 2018 at 10:26
  • 1
    The difference between this code (once you fix the indentation) and the code at gis.stackexchange.com/a/130754/115 is that the latter looks for and adds layers from layer files. Here you seem to be trying to look for and add layers from features classes. I think you should set up a test script that does nothing other than try to add a single layer from a feature class. Once you have that working it will be easier to insert a code snippet that you know works into a larger one. Commented Jun 3, 2018 at 10:39

1 Answer 1

1

Finely, i changed the datatype into "Layer", and backspace the "mxd.save()":

import arcpy,os
workspace = r"G:\desktop\Project\lyr\taba7650円"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\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,'\n' 
mxd.save() 
answered Jun 3, 2018 at 14:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.