1

I try to print all the MXD files name. The MXD files located in one directory (which is divided into folders and sub folders). Also in the sub folders there are MXD files. I work with this code:

import arcpy,os,sys,fnmatch  
from arcpy import env 

rootPath = r"G:\desktop\Project"
for root, dirs, files in os.walk(rootPath):
    env.workspace = root
    for mxdname in arcpy.ListFiles("*.mxd"):
        print root + '\\' + mxdname
        mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\\" + mxdname)
        print mxdname

It print all MXD files name in the main directory, but it don't print MXD files in the sub directory. This the error:

>>> 
G:\desktop\Project\landuse__4___20013.mxd
landuse__4___20013.mxd
G:\desktop\Project\landuse__5___20013.mxd
landuse__5___20013.mxd
G:\desktop\Project\project.mxd
project.mxd
G:\desktop\Project\k20000\landuse__1___20013.mxd

Traceback (most recent call last):
  File "G:\desktop\python.py", line 9, in <module>
  mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\\" + mxdname)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 611, in __init__
   assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.
>>>
4

It's always best to use os.path.join when concatenating paths. Also, you are printing the correct path (root + mxdname), but using the parent directory as the argument to MapDocument.

Try this:

for mxdname in arcpy.ListFiles("*.mxd"):
    fullpath = os.path.join(root, mxdname)
    print fullpath
    mxd = arcpy.mapping.MapDocument(fullpath)
    print mxdname
1

You're not passing the folder name in mxd so it can't find the mxd.

mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\\" + mxdname) won't find the folder k20000 in G:\desktop\Project\k20000\landuse__1___20013.mxd, it's looking for G:\desktop\Project\landuse__1___20013.mxd

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.