<?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: Replace parts of a data source string in Layer files? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666574#M51744</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see the oldPath has "&lt;STRONG&gt;ä&lt;/STRONG&gt;" in the name. Python normally doesn't like that and other functions may not work well with these type of characters (for instance working with the raster calculator). Personally I would reconsider if the path should be named like this (also spaces, points, dashes, etc will be a sources or problems when working with rasters calculations).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckeng&lt;STRONG&gt;ä&lt;/STRONG&gt;u"&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You may have to use the next line as first line in your script:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# -*- coding: utf-8 -*-&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If that doesn't work, maybe you should enclose your script by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys
reload(sys)
# read system settings and set utf8
def_enc = sys.getdefaultencoding()
sys.setdefaultencoding('utf8')

# script logic goes here ...

# restore settings
sys.setdefaultencoding(def_enc)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But my recommendation is to reconsider the folder names&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:10:05 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-12-12T04:10:05Z</dc:date>
    <item>
      <title>Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666569#M51739</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Why does this code replace only things like c:\temp\data through c:\tempXXX\data and not the beginning of c:\temp\data\1\2\3 ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os
# Pfade definieren
folderPath = r"R:\SG-C\8850.20-6 FFH-Gebiete\04 MaP\7218-341_Calwer_Heckengaeu"
oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckengäu"
# mxd definieren
path = r"R:\SG-C\8850.20-6 FFH-Gebiete\04 MaP\7218-341_Calwer_Heckengaeu\7218341.mxd"
mxd = arcpy.mapping.MapDocument(path)&amp;nbsp; 
for df in arcpy.mapping.ListDataFrames(mxd):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd): 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(oldPath, folderPath, "")
arcpy.RefreshTOC()&amp;nbsp; 
arcpy.RefreshActiveView()&amp;nbsp; 
mxd.save
del mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:10:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666569#M51739</guid>
      <dc:creator>JohannesBierer</dc:creator>
      <dc:date>2021-12-12T04:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666570#M51740</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think you may want ListLayers(df), not ListLayers(mxd).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Feb 2015 21:04:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666570#M51740</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2015-02-10T21:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666571#M51741</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can't pass ListLayers only a dataframe object, it will fail.&amp;nbsp; At a minimum, and map document or layer needs to be passed.&amp;nbsp; That said, the code still has an issue.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Feb 2015 21:28:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666571#M51741</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-02-10T21:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666572#M51742</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am not understanding your predicament.&amp;nbsp; Could you post a screenshot or some specific examples of what you would like to see and what you are actually seeing?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regarding your code, you don't have to loop over the dataframes if you aren't going to pass them to ListLayers.&amp;nbsp; Calling ListLayers without a dataframe object will list the layers in all of the dataframes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Feb 2015 21:35:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666572#M51742</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-02-10T21:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666573#M51743</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok, the code was working to replace c:\temp\data through c:\tempXXX\data. But not if there are more subfolders like c:\tempYYY\datax\dataY\dataZ it failed?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have tested it with only listlayer. Somehow it steps out. After printing Projektgewaesser ..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="ReplacePath.PNG" class="jive-image image-1" src="https://community.esri.com/legacyfs/online/60705_ReplacePath.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 695, in findAndReplaceWorkspacePath&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))&lt;/P&gt;&lt;P&gt;ValueError: Layer: Unexpected error&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Feb 2015 06:37:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666573#M51743</guid>
      <dc:creator>JohannesBierer</dc:creator>
      <dc:date>2015-02-11T06:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666574#M51744</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see the oldPath has "&lt;STRONG&gt;ä&lt;/STRONG&gt;" in the name. Python normally doesn't like that and other functions may not work well with these type of characters (for instance working with the raster calculator). Personally I would reconsider if the path should be named like this (also spaces, points, dashes, etc will be a sources or problems when working with rasters calculations).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;oldPath = r"R:\natura 2000\8847.04 MaP\7218-341_Calwer_Heckeng&lt;STRONG&gt;ä&lt;/STRONG&gt;u"&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You may have to use the next line as first line in your script:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# -*- coding: utf-8 -*-&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If that doesn't work, maybe you should enclose your script by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys
reload(sys)
# read system settings and set utf8
def_enc = sys.getdefaultencoding()
sys.setdefaultencoding('utf8')

# script logic goes here ...

# restore settings
sys.setdefaultencoding(def_enc)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But my recommendation is to reconsider the folder names&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:10:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666574#M51744</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T04:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replace parts of a data source string in Layer files?</title>
      <link>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666575#M51745</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, that's correct, my bad. ListLayers(mxd, "", df) would be more correct.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Feb 2015 13:13:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-parts-of-a-data-source-string-in-layer/m-p/666575#M51745</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2015-02-11T13:13:15Z</dc:date>
    </item>
  </channel>
</rss>

