<?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 Layer.saveACopy() failing for .LYR file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555825#M43418</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;From &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/"&gt;here&lt;/A&gt;&lt;SPAN&gt;, the Layer class &lt;/SPAN&gt;&lt;SPAN style="color: &amp;quot;blue&amp;quot;; font-style: italic;"&gt;Provides access to layer properties and methods. It can either reference layers in a map document (.mxd) &lt;STRONG&gt;or layers in a layer (.lyr) file&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to update an existing layer file (.lyr), replacing the data sources and saving to a new file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code below works fine if the source document is an mxd, but fails catastrophically (kills python) if it is a lyr file. (The lyr file was saved from the mxd.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; [INDENT]import arcpy&amp;nbsp; &lt;BR /&gt; &lt;STRONG&gt;sourceType = ".mxd" # Change this to .lyr and it fails.&lt;/STRONG&gt; &lt;BR /&gt; &lt;BR /&gt;fullpath = r"D:\WD\InterimGroundwater\Salinity" + sourceType&amp;nbsp; &lt;BR /&gt;sourceLyr = arcpy.mapping.MapDocument(fullpath)&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;ID = "10014087"&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;newAquiferBoundary = "ID_" + ID + "_AquiferBoundary"&amp;nbsp; &lt;BR /&gt;newLyr = r"D:\WD\InterimGroundwater\Salinity_" + ID + sourceType&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;lyrList = arcpy.mapping.ListLayers(sourceLyr)&amp;nbsp; &lt;BR /&gt;for lyr in lyrList:&amp;nbsp; &lt;BR /&gt; if lyr.supports("DATASOURCE"):&amp;nbsp; &lt;BR /&gt; if lyr.name == "AquiferBoundary" :&amp;nbsp; &lt;BR /&gt; print lyr.dataSource&amp;nbsp; &lt;BR /&gt; lyr.replaceDataSource(r"D:\\WD\\InterimGroundwater\\AHGF_GW_v1-0_master.gdb", "FILEGDB_WORKSPACE", newAquiferBoundary)&amp;nbsp; &lt;BR /&gt;sourceLyr.saveACopy(newLyr)[/INDENT]&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Am I doing something wrong or is this a bug?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm running SP2, Build3200.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 05 Jul 2011 23:31:10 GMT</pubDate>
    <dc:creator>MalcolmParnell2</dc:creator>
    <dc:date>2011-07-05T23:31:10Z</dc:date>
    <item>
      <title>Layer.saveACopy() failing for .LYR file</title>
      <link>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555825#M43418</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;From &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/"&gt;here&lt;/A&gt;&lt;SPAN&gt;, the Layer class &lt;/SPAN&gt;&lt;SPAN style="color: &amp;quot;blue&amp;quot;; font-style: italic;"&gt;Provides access to layer properties and methods. It can either reference layers in a map document (.mxd) &lt;STRONG&gt;or layers in a layer (.lyr) file&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to update an existing layer file (.lyr), replacing the data sources and saving to a new file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code below works fine if the source document is an mxd, but fails catastrophically (kills python) if it is a lyr file. (The lyr file was saved from the mxd.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; [INDENT]import arcpy&amp;nbsp; &lt;BR /&gt; &lt;STRONG&gt;sourceType = ".mxd" # Change this to .lyr and it fails.&lt;/STRONG&gt; &lt;BR /&gt; &lt;BR /&gt;fullpath = r"D:\WD\InterimGroundwater\Salinity" + sourceType&amp;nbsp; &lt;BR /&gt;sourceLyr = arcpy.mapping.MapDocument(fullpath)&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;ID = "10014087"&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;newAquiferBoundary = "ID_" + ID + "_AquiferBoundary"&amp;nbsp; &lt;BR /&gt;newLyr = r"D:\WD\InterimGroundwater\Salinity_" + ID + sourceType&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;lyrList = arcpy.mapping.ListLayers(sourceLyr)&amp;nbsp; &lt;BR /&gt;for lyr in lyrList:&amp;nbsp; &lt;BR /&gt; if lyr.supports("DATASOURCE"):&amp;nbsp; &lt;BR /&gt; if lyr.name == "AquiferBoundary" :&amp;nbsp; &lt;BR /&gt; print lyr.dataSource&amp;nbsp; &lt;BR /&gt; lyr.replaceDataSource(r"D:\\WD\\InterimGroundwater\\AHGF_GW_v1-0_master.gdb", "FILEGDB_WORKSPACE", newAquiferBoundary)&amp;nbsp; &lt;BR /&gt;sourceLyr.saveACopy(newLyr)[/INDENT]&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Am I doing something wrong or is this a bug?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm running SP2, Build3200.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jul 2011 23:31:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555825#M43418</guid>
      <dc:creator>MalcolmParnell2</dc:creator>
      <dc:date>2011-07-05T23:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Layer.saveACopy() failing for .LYR file</title>
      <link>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555826#M43419</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think your problem is with your source layer declaration. (&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;sourceLyr = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;SPAN&gt;) In this statement, you need to be declaring a map document (mxd) and so that's why when you change the extension to .lyr, it crashes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd suggest:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sourceLyr = arcpy.mapping.ListLayers(mxd, "name of layer", "data frame")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and then carry on with declaring the ID and the new layer, etc.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jul 2011 20:13:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555826#M43419</guid>
      <dc:creator>EricaSmith</dc:creator>
      <dc:date>2011-07-13T20:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Layer.saveACopy() failing for .LYR file</title>
      <link>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555827#M43420</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I think your problem is with your source layer declaration. (&lt;SPAN style="font-style:italic;"&gt;sourceLyr = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;) In this statement, you need to be declaring a map document (mxd) and so that's why when you change the extension to .lyr, it crashes.&lt;BR /&gt;&lt;BR /&gt;I'd suggest:&lt;BR /&gt;mxd = arcpy.mapping.MapDocument(fullpath)&lt;BR /&gt;sourceLyr = arcpy.mapping.ListLayers(mxd, "name of layer", "data frame")[0]&lt;BR /&gt;and then carry on with declaring the ID and the new layer, etc.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Erica, but I think you're talking about a layer within an MXD?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I was wanting to create a copy of a .LYR files stored on disk.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Jul 2011 05:36:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-saveacopy-failing-for-lyr-file/m-p/555827#M43420</guid>
      <dc:creator>MalcolmParnell2</dc:creator>
      <dc:date>2011-07-22T05:36:58Z</dc:date>
    </item>
  </channel>
</rss>

