[GIS] AssertionError: Invalid MXD filename from arcpy.mapping.MapDocument()

arcgis-10.1arcpy

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?

Best Answer

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
Related Question