I'm dealing with several layer files that have out-of-date sources; the actual layers refered to in the .lyr file started in L: and have since been copied to W:/FOR/RSI/DKL/Local_Data .
I'm trying to update the .lyr files (in Python 2.7) to point to the new source layers. It should be straightforward, as each .lyr file has a single source, so I'm not (yet) dealing with group layers or anything like that.
My output (see below) tells me that the lyr data source is updated, but checking it in ArcCatalog, I see this is not true.
How do I actually update the dataSource in the layer file?
It seems to me I am simply changing a property of the lyr object.
I have a feeling that my arguments are maybe not quite correct in this line:
lyr.replaceDataSource (newLayerString,"SHAPEFILE_WORKSPACE", layerName)
for layerFile in glob.glob(layerFilePath + "/*.lyr"): print layerFile lyr = arcpy.mapping.Layer(layerFile) # returns the object from each layer file layers = arcpy.mapping.ListLayers(lyr) # returns the list of objects (i.e. the actual layers) referenced by the layer file for layer in layers: if layer.supports("dataSource"): # some layers might not support the property "dataSource" layerString = str(layer.workspacePath) layerName = layer.datasetName print '\t',"Layer ToC name is %s and workspace path is %s" %(layer.name, layer.workspacePath) if layer.dataSource[-4:] == ".shp": workspaceType = "SHAPEFILE_WORKSPACE" #add in other possibilities here for coverage sources, SDE feature classes etc.. if 'L:' in layerString: #this is a better way to select part of a string than an element search i.e. newLayerString = layerString.replace(r"L:",r"W:/FOR/RSI/DKL/Local_Data",1) lyr.replaceDataSource (newLayerString,"SHAPEFILE_WORKSPACE", layerName) print '\t',"The updated source for %s is %s" %(layer.name,lyr.dataSource) del layer, layers #get rid of these objects print""