Select to view content in your preferred language

Layer.ReplaceDataSource

5113
3
Jump to solution
01-08-2015 07:08 AM
GeoffOlson
Regular Contributor

I have a total of 27 mxd files that I need replace the source path for a layer.  I'm trying to use os.walk to go through and update each mxd because they are in subfolders of the main folder.  It seems I have a directory variable problem.  I've set the workspace to the folder with the files, but when I try to use os.path.abspath() Python is prefixing my filename with the directory going to ...Users\..\Documents\. Is there another parameter to change?  I'm receiving an assertion error on an invalid MXD filename, and I think this is the reason why.  For now I have the actual functions coded out until I can get the script to run through the files properly.

import arcpy, os, glob
arcpy.env.workspace = r'Z:\GIS\Projects\Travel_Time_Survey'


mainDir = r'Z:\GIS\Projects\Travel_Time_Survey'
for root, dirs, files in os.walk(mainDir):
    for filename in files:
        print filename
        if filename[-4:] == ".mxd":
            print filename
            filePath = os.path.abspath(filename)
            mxd = arcpy.mapping.MapDocument(filePath)
            for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr == "Roads":
                    print lyr
#                    Lyr.replaceDataSource(r"Z:\GIS\Projects\LRTP", "SHAPEFILE_WORKSPACE", "Streets_LRTP.shp",TRUE)
#                    mxd.save()
#                    arcpy.mapping.ExportToPDF(mxd, r"Z:\GIS\Projects\Travel_Time_Survey\{0}.pdf".format(filename[:-4]))
                else:
                    pass
        else:
            pass
    else:
        pass
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You are misunderstanding what os.path.abspath actually does.  From 10.1. os.path — Common pathname manipulations — Python 2.7.9 documentation:

os.path.abspath(path)
     Return a normalized absolutized version of the pathname path. On most platforms, this is

     equivalent to calling the function normpath() as follows:

     normpath(join(os.getcwd(), path)).

New in version 1.5.2.

You are joining the current working directory of the command prompt or script to the name of the file.  Try replacing line 11 with:

filePath = os.path.join(root, filename)

View solution in original post

0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

You are misunderstanding what os.path.abspath actually does.  From 10.1. os.path — Common pathname manipulations — Python 2.7.9 documentation:

os.path.abspath(path)
     Return a normalized absolutized version of the pathname path. On most platforms, this is

     equivalent to calling the function normpath() as follows:

     normpath(join(os.getcwd(), path)).

New in version 1.5.2.

You are joining the current working directory of the command prompt or script to the name of the file.  Try replacing line 11 with:

filePath = os.path.join(root, filename)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can also use the new-ish Walk (arcpy.da) to find geo-related files and data, including maps.

0 Kudos
GeoffOlson
Regular Contributor

Thanks for the reply.  I forgot about root being a callable variable, too.  I ended up copying all the mxds to the main folder and it ran fine, then I had to move all the files back.  If I run into this issue again it helps to know.  I use a lot of template mxds that can quickly spread to many files that need a change.

0 Kudos