<?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: Save layer file to data source path in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500817#M70976</link>
    <description>&lt;P&gt;Thanks a lot, yes we are almost there but still missing somenthing.&lt;/P&gt;&lt;P&gt;I have modified the code as follow:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject('CURRENT')&lt;BR /&gt;m = aprx.listMaps('Map') [0]&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isGroupLayer:&lt;BR /&gt;arcpy.management.SaveToLayerFile(lyr,'output_path', 'RELATIVE')&lt;/P&gt;&lt;P&gt;The&amp;nbsp; layer iteration is working but is replacing the outputfile with the same name.&lt;/P&gt;&lt;P&gt;I would like to get the output file name with the layer name and in the same folder of the source data. Is it possible?&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2024 08:58:11 GMT</pubDate>
    <dc:creator>FabioTuccini</dc:creator>
    <dc:date>2024-07-03T08:58:11Z</dc:date>
    <item>
      <title>Save layer file to data source path</title>
      <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500749#M70974</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to write a code to save the layer file (.lyrx) for each layer present in my project in the same folder of the data source.&lt;/P&gt;&lt;P&gt;At the moment I can only one by one, with the following code:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;arcpy.mp.ArcGISProject('CURRENT')&lt;BR /&gt;arcpy.management.SaveToLayerFile('layer','output_path', 'RELATIVE')&lt;/P&gt;&lt;P&gt;But I wold like to create a iteration with all&amp;nbsp; layers in my project. It's important that each .lyrx will be saved in the data source path.&lt;/P&gt;&lt;P&gt;Someone can help please&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 05:26:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500749#M70974</guid>
      <dc:creator>FabioTuccini</dc:creator>
      <dc:date>2024-07-03T05:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Save layer file to data source path</title>
      <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500812#M70975</link>
      <description>&lt;P&gt;You are almost there, just put your code in the layer iteration:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;m = aprx.listMaps("map name")[0]
for lyr in m.listLayers():
    if !lyr.isGroupLayer:
        arcpy.management.SaveToLayerFile(lyr,'output_path', 'RELATIVE')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Something like above, but I haven't tested code myself.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 07:52:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500812#M70975</guid>
      <dc:creator>simoxu</dc:creator>
      <dc:date>2024-07-03T07:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Save layer file to data source path</title>
      <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500817#M70976</link>
      <description>&lt;P&gt;Thanks a lot, yes we are almost there but still missing somenthing.&lt;/P&gt;&lt;P&gt;I have modified the code as follow:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject('CURRENT')&lt;BR /&gt;m = aprx.listMaps('Map') [0]&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isGroupLayer:&lt;BR /&gt;arcpy.management.SaveToLayerFile(lyr,'output_path', 'RELATIVE')&lt;/P&gt;&lt;P&gt;The&amp;nbsp; layer iteration is working but is replacing the outputfile with the same name.&lt;/P&gt;&lt;P&gt;I would like to get the output file name with the layer name and in the same folder of the source data. Is it possible?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 08:58:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500817#M70976</guid>
      <dc:creator>FabioTuccini</dc:creator>
      <dc:date>2024-07-03T08:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: Save layer file to data source path</title>
      <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500836#M70977</link>
      <description>&lt;P&gt;OK, I'm very close!&lt;/P&gt;&lt;P&gt;I've modified the code as follow:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject('CURRENT')&lt;BR /&gt;m = aprx.listMaps('Map') [0]&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isFeatureLayer:&lt;BR /&gt;arcpy.management.SaveToLayerFile(lyr,lyr.dataSource + lyr.name, 'RELATIVE')&lt;/P&gt;&lt;P&gt;But I get the following error message:&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 6, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 11180, in SaveToLayerFile&lt;BR /&gt;raise e&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 11177, in SaveToLayerFile&lt;BR /&gt;retval = convertArcObjectToPythonObject(gp.SaveToLayerFile_management(*gp_fixargs((in_layer, out_layer, is_relative_path, version), True)))&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in &amp;lt;lambda&amp;gt;&lt;BR /&gt;return lambda *args: val(*gp_fixargs(args, True))&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.&lt;BR /&gt;ERROR 000725: Output Layer: Dataset C:\Users\Fabio\Documents\ArcGIS\Projects\PRY002_WTL\03_Shape\Routes\Rerouting\VAB15_A.shpVAB15_A already exists.&lt;BR /&gt;Failed to execute (SaveToLayerFile).&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 10:16:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500836#M70977</guid>
      <dc:creator>FabioTuccini</dc:creator>
      <dc:date>2024-07-03T10:16:13Z</dc:date>
    </item>
    <item>
      <title>Re: Save layer file to data source path</title>
      <link>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500900#M70981</link>
      <description>&lt;P&gt;Finally i found the solution. Maybe to someone can help, here the final code:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject('CURRENT')&lt;BR /&gt;m = aprx.listMaps('Map') [0]&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isFeatureLayer:&lt;BR /&gt;arcpy.management.SaveToLayerFile(lyr,os.path.dirname(lyr.dataSource)+"\\"+lyr.name, 'RELATIVE')&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 11:58:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/save-layer-file-to-data-source-path/m-p/1500900#M70981</guid>
      <dc:creator>FabioTuccini</dc:creator>
      <dc:date>2024-07-03T11:58:32Z</dc:date>
    </item>
  </channel>
</rss>

