0

I'm trying to write a script that allows me to list the layers of each map document in a folder. However I keep getting this error after it displays the layers in the first map document:

Traceback (most recent call last): File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in main.dict File "C:\Users\Daimon Nurse\Desktop\DFMPROJECT\Scripts\editmapdocument8.py", line 10, in mxd = arcpy.mapping.MapDocument(file) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 608, in init assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename") AssertionError: Invalid MXD filename.

This is the code I have tried:

import arcpy
import os

PATH2 = r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT"
arcpy.env.workspace = PATH2
arcpy.env.overwriteOutput=True

for file in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.mapping.MapDocument(file) 
    lyr = arcpy.mapping.ListLayers(mxd)
    print lyr

It worked for only the first iteration. Why?

1
  • Do you know the file name that causes the error? I'd add a print statement before the mxd line, so you can see the name of the file. Commented Jan 10, 2015 at 0:25

1 Answer 1

5

You need to pass the full path of the mxd to the arcpy.mapping.MapDocument() function

import arcpy
import os

PATH2 = r"C:\Users\Daimon Nurse\Desktop\DFMPROJECT"
arcpy.env.workspace = PATH2
arcpy.env.overwriteOutput=True

for file in arcpy.ListFiles("*.mxd"):
  mxd_path = os.path.join(PATH2,file)
  mxd = arcpy.mapping.MapDocument(mxd_path) 
  lyr = arcpy.mapping.ListLayers(mxd)
  print lyr
1
  • 1
    Better yet, mxd_path = os.path.join(arcpy.env.workspace, file). More resistant to future changes to the script. Commented Dec 5, 2018 at 20:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.