5

I try to use 'findAndReplaceWorkspacePaths' function on MXD files using ArcView 10.3.1. The MXD files located in one directory (which is divided into folders and sub folders). Also in the sub folders there are MXD files.

This is the directory content location:

enter image description here

and this is the sub folders location (there are more MXD files in the other folders):

enter image description here


I write in python 2.7:

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

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

The result is:

>>> 
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePathslanduse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
landuse__3___20013.mxd
findAndReplaceWorkspacePaths
landuse__4___20013.mxd
findAndReplaceWorkspacePaths
>>> 

Now, there are two problems:

  1. Why the code work only with the MXD files in the directory and not in the sub folders?
  2. Why the code printed so many lines?
2
for root, dirs, files in os.walk(rootPath):
    env.workspace = r"G:\desktop\Project"

It's looping through the sub folders, but you keep setting your environment back to the root directory. Try:

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(mxdname)  
        mxd.findAndReplaceWorkspacePaths(r"D:\PROJECTS\green_line\gis\layers", r"G:\PROJECTS\green_line\gis\layers")
        print 'findAndReplaceWorkspacePaths'
        mxd.save()

And even though it's in almost every ESRI example, you don't need that final del mxd command.

  • i get an error :>>> landuse__4___20013.mxd findAndReplaceWorkspacePaths landuse__5___20013.mxd findAndReplaceWorkspacePaths landuse__1___20013.mxd Traceback (most recent call last): File "g:\Users\yaron\Desktop\findAndReplaceWorkspacePaths test.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. – newGIS Feb 18 '16 at 15:23
  • the code don't loop in the MXD files in "20000" folder, just in the major directory – newGIS Feb 18 '16 at 15:27
  • I edited my answer with a fix to one other bug in the arcpy.mapping.MapDocument call. Since you are setting your workspace in each loop, you don't need the full path in the mxdname. – Mintx Feb 18 '16 at 15:41
  • i get an error: >>> G:\desktop\Project/landuse__4___20013.mxd Traceback (most recent call last): File "g:\Users\yaron\Desktop\findAndReplaceWorkspacePaths test.py", line 9, in <module> mxd = arcpy.mapping.MapDocument(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. >>> – newGIS Feb 21 '16 at 6:29
  • when i just print mxdname after the for line and without the oter lines , it print all the mxd files just fine. i don't understand what's wrong in the code – newGIS Feb 21 '16 at 13:52

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.