I'm trying to write a code for automating changing the source for some layers of a mxd document. I have three layers that should point to the same shapefile, they are only displayed with different symbology in the map. So, here is a version of what I tried:
#in this example, the shapefile I want is "C:\\Users\\USER_NAME\\folder\\new_shape.shp" #this new shapefile IS NOT in the same directory as the previous one #also, I tried first changing only one layer import arcpy mxd = arcpy.mapping.MapDocument('current') principal = arcpy.mapping.ListDataFrames(mxd) lyr1 = arcpy.mapping.ListLayers('current', '', principal) lyr2 = arcpy.mapping.ListLayers('current', '', principal) lyr3 = arcpy.mapping.ListLayers('current', '', principal) lyr1.replaceDataSource("C:\\Users\\USER_NAME\\folder", "SHAPEFILE_WORKSPACE", 'new_shape')
From all the examples I found, this should do the trick. However, when I open the MXD, the layer's datasource is the same as before. This happens for any of the three layers.
I really couldn't figure out what is wrong in this code.
I'm using ArcGIS 10.5 and Python 2.7
mxd.save()? I didn't add it to this sample of code here, but I have used it.
validateparameter, the default value of
Trueis used, which is documented as "If set to True, a workspace will only be updated if the workspace_path value is a valid workspace. If it is not valid, the workspace will not be replaced. If set to False, the method will set the source to match the workspace_path, regardless of a valid match. In this case, if a match does not exist, then the data source would be broken." so your replacement is incorrect in some way. Not including the
.shpsuffix could be a cause.