I'm very new to python so please forgive me if this is a simple error. I have quite a few MXD's in which each have a few layers referencing the same source. However I have since moved this data to a new folder. I have written a script to automatically refresh the similar layers.
The script runs through, however when I check to see if its worked, none of the layers are refreshed. I'm not sure whats wrong.
import arcpy, os
#workspace to search for MXDs
Workspace = r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\MXD"
arcpy.env.workspace = Workspace
#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")
print mxdList
#set new link for specific shapefile in each MXD in list.
for file in mxdList:
#set map document to change
filePath = os.path.join(Workspace, file)
mxd = arcpy.mapping.MapDocument(filePath)
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\MMO_South_Plan_Areas.shp":
lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")
if lyr.dataSource == r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1\Land.shp":
lyr.findAndReplaceWorkspacePath(r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\WP1", r"M:\TEAM_GIS\EDKM\Projects\mmo1065_south_habitat_mapping\20140708_Update_3_QA\3_Working_GIS_Files\SHP")
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
print "Success"
for
loops look like they would throw unexpected indentation errors - have you tried running precisely the code that you have posted?print
statements within the code blocks for each test that you perform so that you will know whether or not they are being run. That print output should accompanying the code to help us interpret it too.for lyr in arcpy.mapping.ListLayers(mxd):
loop will only run on the final mxd as it is not indented to be inside thefor file in mxdList:
loop.