Select to view content in your preferred language

Python issue finding and replacing file path's for mxd's in a loop

1043
2
08-03-2012 10:11 AM
MattTenold
Deactivated User
Hi everyone,

I am trying to find and replace workspace path's for a person changing root folder names.  The main issue is that all of the maps are pointed to shapefile in multiple folders and folders inside of eachother.   I am trying to write a python script that will loop through each subfolder looking for the shapefiles' source path and repairing the data path to the new root folder name, since the folder structure will remain the same other than the root folder name changing.

Here is my current code.  Any help would be much appreciated thanks.

import arcpy
import os
rootdir = r"G:\Working\Matt Tenold\*"
for subdir, dirs, files in os.walk(rootdir):
for filename in files:
  fullpath = os.path.join(subdir, filename)
  print filename
  if os.path.isfile(fullpath):
      basename, extension = os.path.splitext(fullpath)
      if extension.lower() == ".mxd":
   mxd = arcpy.mapping.MapDocument(fullpath)
    # Search-and-replace to fix up data source paths.
    mxd.findAndReplaceWorkspacePaths(r"G:\Working\MattTenold\*", r"G:\Working\Matt Tenold\*", False)
   mxd.save()

del df
del mxd
Tags (2)
0 Kudos
2 Replies
MikeHunter
Frequent Contributor
I see what you are trying to do, but neither os.walk or findAndReplaceWorkspacePaths works with wildcard syntax like '*'.  First drop the * from os.walk, and drop the isfile call, since it's not needed.   Then in each mxd you find, you'll have to iterate through the layers and tables, get the workspaces of the data sources, and replace one by one with replaceWorkspaces.

If it were me, I wouldn't change the path without spaces to one with spaces.  That can cause trouble if you are not careful, both in scripts and toolbox tools.  So I'd drop this entire project, unless there's a really, really good reason for doing it.

good luck,
Mike
0 Kudos
ChristopherThompson
Frequent Contributor
since the folder structure will remain the same other than the root folder name changing

If this is the case, then maybe the better approach is to make sure your MXDs are all using relative paths instead of going to the labor of changing the filepath for everything.  To do this with python you might be able to use the relativePaths property on the MapDocument, looping through each mxd and setting that value to true. You have to be careful though that the relationship of that map to all data sources is indeed static or the relative paths capability won't work for you.

So this could be as easy as something like this:

maplist = ['mxd_a','mxd_b'... ] #a list of map documents, however you compile this
for map in maplist:
    mxd = arcpy.mapping.MapDocument(map)
    mxd.relativePath = 'True'


One thing, you probably want to do this before the root folders have been changed, as otherwise I don't think this approach will work if the links have already been broken.  I'd also ditto the other respondent's remark about changing workspace names to include spaces; generally this is a bad idea as there seem to be a lot of cases where arcGIS tools still rely on technologies that don't like spaces or long (> 13 characters or so) in file or path names.  Windows maybe ok with it these things, but ArcGIS not so much.