I've got code to walk through an entire directory and sub-directories repair broken links in each mxd and save the mxd. It works for the mxd's in the top directory but fails when trying to enter any sub.

My code is as follows:

import arcpy, os
from arcpy import env
arcpy.env.overwriteOutput = True
ws = "C:\Users\mittcla\Desktop\New folder"
oldpath = r"C:\NewProjects"
oldbase = r"C:\Base Data"
newpath = r"C:\Users\mittcla\Documents\NewProjects"
newbase = r"C:\Users\mittcla\Documents\Base Data"
for root, dirs, files in os.walk(ws):
    for f in files:
        if f.endswith('.mxd'):
            fullpath = os.path.join(ws,f)
            mxd = arcpy.mapping.MapDocument(fullpath)
            print "Replacing path for " + f +"..."
            mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
            mxd.findAndReplaceWorkspacePaths(oldbase, newbase)
            del mxd

I get an error saying

Runtime error Traceback (most recent call last): File "", line 14, in File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 609, in init assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename") AssertionError: Invalid MXD filename.

What is causing it to break when entering the new directory?

  • You seem to be using 10.2 so I am wondering if you considered using arcpy.da.Walk?
    – PolyGeo
    Jun 11, 2014 at 20:13
  • @PolyGeo if you're just looking for files on the filesystem and not GIS data in particular you might as well use os.walk. Jun 11, 2014 at 20:46

1 Answer 1


The files do not exist as you are building the path string based on the starting directory, not the current one.

fullpath = os.path.join(ws,f)

should be

fullpath = os.path.join(root, f)

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.