0

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\taba\7650"
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

  • 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. – PolyGeo Jun 3 '18 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. – PolyGeo Jun 3 '18 at 10:39
0

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

import arcpy,os
workspace = r"G:\desktop\Project\lyr\taba\7650"
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() 

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.