Store relative path for map documents in sub folders using Python

994
4
Jump to solution
11-29-2017 10:57 AM
Brian_McLeer
Occasional Contributor II

I am trying to find a way to set relative paths in MXDs using Python for several sub folders within a main folder. Using this Tech Support article, it allows it to be done on MXDs in the folder listed as the workspace. What I am looking for is not only the folder listed in the workspace, but MXDs that exist in subfolders. The goal would be for the Python to open all MXDs under the parent folder and subfolder, check on Set Relative Path Name, Save and Close MXD. 

Brian
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

I think it would be more like this, but I haven't tested the code:

import os
from glob import glob
import arcpy

PATH = r'C:\Users\mclbr\Desktop\GIS_Maintenance'
arcpy.env.workspace = PATH

result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for map in result:  # map will be complete path to map file
    print "Processing: {}".format(map) # keep track of what's happening
    mxd = arcpy.mapping.MapDocument(map)
    #set relative paths property
    mxd.relativePaths = True
    #save map doucment change
    mxd.save()‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can add some additional print statements to help keep track of what's happening and to assist with debugging.

I'm not sure how the arcpy workspace plays into relative paths.  You may need to move this into your for loop and set workspace equal to the path up to your mxd file.

View solution in original post

4 Replies
RandyBurton
MVP Regular Contributor

Try this to find your mxd files:

import os
from glob import glob
PATH = r'C:\Start\Here'
result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for mxd in result:
    print mxd # continue here with processing your files‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Brian_McLeer
Occasional Contributor II

So combined code would be:

import os
import arcpy, os
from glob import glob
PATH = r'C:\Users\mclbr\Desktop\GIS_Maintenance'
result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for mxd in result:
 print mxd # continue here with processing your files


import arcpy, os
#workspace to search for MXDs
Workspace = r"C:\Users\mclbr\Desktop\GIS_Maintenance"
arcpy.env.workspace = Workspace
#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")
#set relative path setting for each MXD in list.
for file in mxdList:
 #set map document to change
 result = os.path.join(Workspace, file)
 mxd = arcpy.mapping.MapDocument(filePath)
 #set relative paths property
 mxd.relativePaths = True
 #save map doucment change
 mxd.save()
Brian
0 Kudos
RandyBurton
MVP Regular Contributor

I think it would be more like this, but I haven't tested the code:

import os
from glob import glob
import arcpy

PATH = r'C:\Users\mclbr\Desktop\GIS_Maintenance'
arcpy.env.workspace = PATH

result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for map in result:  # map will be complete path to map file
    print "Processing: {}".format(map) # keep track of what's happening
    mxd = arcpy.mapping.MapDocument(map)
    #set relative paths property
    mxd.relativePaths = True
    #save map doucment change
    mxd.save()‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can add some additional print statements to help keep track of what's happening and to assist with debugging.

I'm not sure how the arcpy workspace plays into relative paths.  You may need to move this into your for loop and set workspace equal to the path up to your mxd file.

DanPatterson_Retired
MVP Esteemed Contributor

Brian.... Code Fomatting the Basics++

to make your code easier to read like Randy's