<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: replaceDataSource in layer file (not mxd) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333757#M26009</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How about changing True to False in the following line?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 08 May 2013 17:16:03 GMT</pubDate>
    <dc:creator>MichaelVolz</dc:creator>
    <dc:date>2013-05-08T17:16:03Z</dc:date>
    <item>
      <title>replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333753#M26005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My world: Win7, ArcGIS 10.0&amp;nbsp; SP1, Python 2.6.5&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am attempting to update a group layer file with new data source locations (FGDB moved from one location to another location). I get no errors running this code, but the new file locations are not updated in the layer file.&amp;nbsp; Most of the discussions in this forum (related to this issue) are related to using an mxd, but in my case, I want to update a layer file (*.lyr).&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Have verified that the paths are correct (via print statements). &lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Have verified that i am updating feature classes and that they support the DATASOURCE property&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Have also tried saving the .lyr file using "arcpy.mapping.Layer(inLayerFile).saveACopy("newfilename")" &lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;I have tried findAndReplaceWorkspacePath with no success.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Have tried converting newPath variable to unicode - again no success.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;SPAN&gt;Any help greatly appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# xModifyDataLocationsInlayerFiles.py&amp;nbsp; import sys, os import arcpy&amp;nbsp; outFolder = r"C:\Projects\CoreData\GeoName" newFolder = r"C:\projects\testmove\test2\GeoName"&amp;nbsp; inLayerFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.lyr"&amp;nbsp; for lyr in arcpy.mapping.Layer(inLayerFile): &amp;nbsp; pout = lyr.isFeatureLayer &amp;nbsp; if pout == True: &amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.supports("DATASOURCE"): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currWSPath = str(lyr.workspacePath) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scol = str.upper(currWSPath).find('GEONAME') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scol = scol + len('GEONAME') + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newPath = newFolder + os.sep + currWSPath[scol:] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.name + " | Path: " + newPath + " | DSN: " + lyr.datasetName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Layer " + lyr.name + " does not support datasource changes" &amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 15:16:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333753#M26005</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2013-05-08T15:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333754#M26006</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In the following line, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;can you remove unicode?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is the lyr.datasetName changing?&amp;nbsp; If not, then leave this parameter blank (e.g. "").&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you print out the variable newPath and post the result in your thread?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 15:59:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333754#M26006</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-05-08T15:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333755#M26007</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I believe you have to save the layer file to make the changes permanent.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 16:02:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333755#M26007</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-05-08T16:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333756#M26008</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Matthew..as per my initial post I tried doing a saveAs but just got a copy of the original (un-changed) .lyr file&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Michael .. removing unicode has no impact (tried that already).&amp;nbsp; Leaving lyr.datasetName blank has no impact.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a summary of the first few lines of output (there are 40 some feature layers that get updated). Data below are:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;lyr.name (layer name)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;newPath (full path to location of data - these are all FGDBs)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;lyr.datasetName: the data set name&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;BR /&gt;&lt;SPAN&gt;---------------------------------&amp;nbsp; output : ------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Provincial_Boundary | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: Provincial_Boundary&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Forest Management Units | New Path: C:\projects\testmove\test2\GeoName\FMU\FMU.gdb | DSN: FMU&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Forest Management Agreement Areas | New Path: C:\projects\testmove\test2\GeoName\FMA\FMA.gdb | DSN: FMA&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ESRD_Regions | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: ESRD_Regions&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Fish_Management_Zone | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: Fish_Management_Zone&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Fish_Watershed_Unit | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: Fish_Watershed_Unit&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wildlife_Management_Unit | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: Wildlife_Management_Unit&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Green/White Area | New Path: C:\projects\testmove\test2\GeoName\GEOADMIN\GEOADMIN.gdb | DSN: Green_White_Area&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;------------------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The attached snapshot (from ArcCatalog shows that the FGDB and feature class exist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24133[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any other ideas greatly welcomed!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 17:01:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333756#M26008</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2013-05-08T17:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333757#M26009</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How about changing True to False in the following line?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 17:16:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333757#M26009</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-05-08T17:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333758#M26010</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How about changing True to False in the following line?&lt;BR /&gt;&lt;BR /&gt;lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Sadly, no impact whether i use True or False.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the suggestions, tho.&amp;nbsp; Appreciate it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 May 2013 17:21:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333758#M26010</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2013-05-08T17:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333759#M26011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Update:&amp;nbsp; I can get the layer references to update if I run this through an MXD. However, that does not update the .lyr file.&amp;nbsp; I also cannot save the group layer file from this mxd (well, I can save the individual layers to individual layer files, but I want one layer file with all the group layer structures intact).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This code works and the new mxd has all the updated paths.&amp;nbsp; But the updates are in the mxd and I still cannot get the replaceDataSource to work on the actual .lyr file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

outFolder = r"C:\Projects\CoreData\GeoName"
newFolder = r"C:\projects\testmove\test2\GeoName"

inMXDFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.mxd"
outMXDFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo_updated.mxd"

mxd = arcpy.mapping.MapDocument(inMXDFile)
mxd.findAndReplaceWorkspacePaths(outFolder,newFolder, False)
mxd.saveACopy(outMXDFile)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:48:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333759#M26011</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2021-12-11T15:48:00Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333760#M26012</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You will need to replacedatasource for the individual layer files in the group layer and then do a save of the group layer that holds the individual layers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try either one of these lines of code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.save() - I think you would put in inLayerFile in () but I'm not sure that will work as the ESRI documentation just shows it blank&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.saveACopy(inLayerFile)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The documentation for this is found at:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 May 2013 15:56:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333760#M26012</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-05-09T15:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333761#M26013</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You will need to replacedatasource for the individual layer files in the group layer and then do a save of the group layer that holds the individual layers.&lt;BR /&gt;&lt;BR /&gt;Try either one of these lines of code:&lt;BR /&gt;&lt;BR /&gt;lyr.save() - I think you would put in inLayerFile in () but I'm not sure that will work as the ESRI documentation just shows it blank&lt;BR /&gt;&lt;BR /&gt;lyr.saveACopy(inLayerFile)&lt;BR /&gt;&lt;BR /&gt;The documentation for this is found at:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Michael.&amp;nbsp; I have tried the lyr.save INSIDE the lyr loop (as it processes the individual feature layers) and that did not help&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also tried "arcpy.mapping.Layer(inLayerFile).saveACopy(outLayerFile)" (after the lyr loop is finished) also to no avail.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have submitted this to the ESRI Canada support team as an incident, so will see how they respond.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you ... appreciate all your help and ideas. Will keep the thread posted with ESRI Canada results.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 May 2013 16:22:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333761#M26013</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2013-05-09T16:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333762#M26014</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I thought one possibility was how the layer file was created: from an MXD with or without relative paths.&amp;nbsp; I tested changeing data sources on group layers both ways and it is working for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
fullLyr = arcpy.mapping.Layer(r"C:\Temp\FullPath.lyr")
lyrs = arcpy.mapping.ListLayers(lyr1)
for lyr in lyrs:
&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data")
fullLyr.saveACopy(r"C:\Temp\FullPath2.lyr")

relLyr = arcpy.mapping.Layer(r"C:\Temp\RelPath.lyr")
lyrs = arcpy.mapping.ListLayers(lyr2)
for lyr in lyrs:
&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data")
relLyr.saveACopy(r"C:\Temp\RelPath2.lyr")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:48:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333762#M26014</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2021-12-11T15:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333763#M26015</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Brian:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A slightly modified version of Jeff's script worked for me where I switched out findAndReplaceWorkspacePath with replaceDataSource to get a group layer to have its connection properties updated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might want to simplify your script to get it to work and then attempt to rebuild it close to its existing structure.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 May 2013 12:09:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333763#M26015</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-05-10T12:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333764#M26016</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Jeff. Your code references objects "lyr1" and "lyr2" but these are not defined in your script. At first I assumed these would be "fullLyr" and "relLyr", but the function ListLayers doesn't support layer files, it seems to only support map documents. I &lt;/SPAN&gt;&lt;STRONG&gt;can &lt;/STRONG&gt;&lt;SPAN&gt;get this to work inside a map document, but I was hoping to avoid using an MXD if possible.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, I need to get moving beyond this, so my solution is simply to open a blank mxd, add my layer file, update the source using .findAndReplaceWorkspacePath, then save the top layer of the layers in the MXD. Since I control the creation of the&amp;nbsp; layer file to begin with I know that I can save this top layer and I get the expected result.&amp;nbsp; Then I just delete the temporary mxd file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you all for your help.&amp;nbsp; My final code (which works properly) is posted here, but sadly I did have to resort to using an MXD:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; outFolder = r"C:\Projects\CoreData\GeoName" newFolder = r"\\silver\projects\SpatialData_Core\GeoName"&amp;nbsp; inLayerFile = r"C:\Projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.lyr" outLayerFile = r"\\silver\projects\SpatialData_Core\GeoName\LayerFiles\AG10\BaseInfo_updated.lyr" inMXDFile = r"C:\Projects\CoreData\GeoName\LayerFiles\AG10\BaseInfo.mxd" outMXDFile = r"\\silver\projects\SpatialData_Core\GeoName\LayerFiles\AG10\BaseInfo_updated.mxd"&amp;nbsp; if arcpy.Exists(outMXDFile): &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(outMXDFile) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(outLayerFile): &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(outLayerFile)&amp;nbsp; mxd = arcpy.mapping.MapDocument(inMXDFile) mxd.findAndReplaceWorkspacePaths(outFolder,newFolder, False) mxd.saveACopy(outMXDFile)&amp;nbsp; del mxd mxd = arcpy.mapping.MapDocument(outMXDFile) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "", df)[0] lyr.saveACopy(outLayerFile)&amp;nbsp; if arcpy.Exists(outMXDFile): &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(outMXDFile)&amp;nbsp; print "Finished creating new Layer file: " + outLayerFile&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 May 2013 15:09:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333764#M26016</guid>
      <dc:creator>BrianMaier</dc:creator>
      <dc:date>2013-05-13T15:09:54Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource in layer file (not mxd)</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333765#M26017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm sorry Brian.&amp;nbsp; I tried to clean up the variable names so they would be more meaningful.&amp;nbsp; I should stop doing that after I have the code working.&amp;nbsp; Here is the corrected code.&amp;nbsp; ListLayers does work on layer files.&amp;nbsp; Think of a layerfile as being a container of layers, just like an MXD.&amp;nbsp; In many cases a layer file may only be a single layer but when you have group layers, it can be many layers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
fullLyr = arcpy.mapping.Layer(r"C:\Temp\FullPath.lyr")
lyrs = arcpy.mapping.ListLayers(fullLyr)
for lyr in lyrs:
&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data")
fullLyr.saveACopy(r"C:\Temp\FullPath2.lyr")

rellLyr = arcpy.mapping.Layer(r"C:\Temp\RelPath.lyr")
lyrs = arcpy.mapping.ListLayers(rellLyr)
for lyr in lyrs:
&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data")
relLyr.saveACopy(r"C:\Temp\RelPath2.lyr")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this helps you,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:48:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource-in-layer-file-not-mxd/m-p/333765#M26017</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2021-12-11T15:48:05Z</dc:date>
    </item>
  </channel>
</rss>

