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?

  • 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. – mr.adam Jan 10 '15 at 0:25
3

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
  • Better yet, mxd_path = os.path.join(arcpy.env.workspace, file). More resistant to future changes to the script. – jpmc26 Dec 5 '18 at 20:27

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.