AnsweredAssumed Answered


Question asked by GEOlson86 on Jan 8, 2015
Latest reply on Jan 8, 2015 by GEOlson86

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)
#                    arcpy.mapping.ExportToPDF(mxd, r"Z:\GIS\Projects\Travel_Time_Survey\{0}.pdf".format(filename[:-4]))