1

My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.

Does anyone have an idea?

import arcpy,os
arcpy.env.workspace = ws = r"C:\Users\Rachel\Desktop\Example\Wetland.shp"
wetland = r"C:\Users\Rachel\Desktop"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list
1
  • 3
    You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
    – ahmadhanb
    Commented Dec 6, 2018 at 1:22

2 Answers 2

3

There are many issues in your code:

  1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
  2. No indent after for loop

Here is a working code that I used

import arcpy,os
arcpy.env.workspace = r"C:\Users\Rachel\Desktop"
ws = arcpy.env.workspace
wetland = r"C:\Users\Rachel\Desktop\Example\Wetland.shp"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

del mxd_list

The above code assumes that the mxd files are stored inside r"C:\Users\Rachel\Desktop" workspace.

2
  • 1
    I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:\some\path' as it actually worked, I can think of a few times this quirk could come in handy. Commented Dec 6, 2018 at 1:40
  • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
    – ahmadhanb
    Commented Dec 6, 2018 at 1:43
2

You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.

@ahmadhanb is correct, you're transposing your workspace and wetland:

import arcpy,os
arcpy.env.workspace = ws = r"C:\Users\Rachel\Desktop"
wetland = r"C:\Users\Rachel\Desktop\Example\Wetland.shp"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

del mxd_list

However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:\GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.

Your Answer

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

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