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:
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
1 Answer 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
lang-py
filenames:arcpy.mapping
.