How To: Set relative paths for all map documents in a folder using Python


The map document properties provides the option to set relative paths for layers within the map document. The paths are stored as incomplete paths that are relative to the current location of the map document.


Relative paths can be set by opening the properties of a map document and choosing the option to store the relative paths of the data sources. This process can be time consuming if done one document at a time. However, the following Python script automates this process for all map documents within a folder.

import arcpy, os

#workspace to search for MXDs
Workspace = r"c:\Temp\MXDs"
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
filePath = os.path.join(Workspace, file)
mxd = arcpy.mapping.MapDocument(filePath)
#set relative paths property
mxd.relativePaths = True
#save map doucment change

If it is desired to have all new maps stored with relative paths, it is necessary to specify relative paths as the default. Click Customize > ArcMap Options to open the ArcMap Options dialog box. Click the General tab. Check the option to 'Make relative paths the default for new map documents'.

Related Information