Skip to main content
idfg-badge

Idaho Fish and Game

Update the data source in an ArcMap project using python, arcpy.mapping

idfg-aschmidt

Need to update a path in your ArcMap project to reflect the new location?  Here's how...

There are several methods for updating data sources using arcpy.mapping, check out this help topic from ESRI.  The example listed below is just one of several methods.

 Attach the following python code to a script in the toolbox of the mxd you wish to update.  This will allow you to update the directory the data layer is referencing, but will NOT allow you to change the data layer source name itself.  To change the data layer source name use the 'replaceDataSource' method instead.

###################

import arcpy
mapdoc = arcpy.mapping.MapDocument("CURRENT")
mapdoc.findAndReplaceWorkspacePaths("" , "")
mapdoc.save()
del mapdoc

###################

TIPS / Notes:

--You could get fancy and set the 'directory you are changing from' and the 'directory you are changing to' to input arguments.  Then, the script could be used for resetting any project.

--If you don't know the directories your data are referencing you can insert print lyr.dataSource.  This will  return a list of data sources used in the mxd.

--If you know you have broken links in your mxd, here's how to get a list printed to the python shell window.

import arcpy
mapdoc = arcpy.mapping.MapDocument("CURRENT")
brokenlist = arcpy.mapping.ListBrokenDataSources(mapdoc)

      for lyr in brokenlist:      

      print lyr.name

del mapdoc