<?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 Python issue finding and replacing file path's for mxd's in a loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402132#M31666</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to find and replace workspace path's for a person changing root folder names.&amp;nbsp; The main issue is that all of the maps are pointed to shapefile in multiple folders and folders inside of eachother.&amp;nbsp;&amp;nbsp; I am trying to write a python script that will loop through each subfolder looking for the shapefiles' source path and repairing the data path to the new root folder name, since the folder structure will remain the same other than the root folder name changing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my current code.&amp;nbsp; Any help would be much appreciated thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rootdir = r"G:\Working\Matt Tenold\*"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for subdir, dirs, files in os.walk(rootdir):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; for filename in files:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; fullpath = os.path.join(subdir, filename)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; print filename&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if os.path.isfile(fullpath):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if extension.lower() == ".mxd":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Search-and-replace to fix up data source paths.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.findAndReplaceWorkspacePaths(r"G:\Working\MattTenold\*", r"G:\Working\Matt Tenold\*", False)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del df&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 03 Aug 2012 17:11:35 GMT</pubDate>
    <dc:creator>MattTenold</dc:creator>
    <dc:date>2012-08-03T17:11:35Z</dc:date>
    <item>
      <title>Python issue finding and replacing file path's for mxd's in a loop</title>
      <link>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402132#M31666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to find and replace workspace path's for a person changing root folder names.&amp;nbsp; The main issue is that all of the maps are pointed to shapefile in multiple folders and folders inside of eachother.&amp;nbsp;&amp;nbsp; I am trying to write a python script that will loop through each subfolder looking for the shapefiles' source path and repairing the data path to the new root folder name, since the folder structure will remain the same other than the root folder name changing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my current code.&amp;nbsp; Any help would be much appreciated thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rootdir = r"G:\Working\Matt Tenold\*"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for subdir, dirs, files in os.walk(rootdir):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; for filename in files:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; fullpath = os.path.join(subdir, filename)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; print filename&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if os.path.isfile(fullpath):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if extension.lower() == ".mxd":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Search-and-replace to fix up data source paths.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.findAndReplaceWorkspacePaths(r"G:\Working\MattTenold\*", r"G:\Working\Matt Tenold\*", False)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del df&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2012 17:11:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402132#M31666</guid>
      <dc:creator>MattTenold</dc:creator>
      <dc:date>2012-08-03T17:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: Python issue finding and replacing file path's for mxd's in a loop</title>
      <link>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402133#M31667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I see what you are trying to do, but neither os.walk or findAndReplaceWorkspacePaths works with wildcard syntax like '*'.&amp;nbsp; First drop the * from os.walk, and drop the isfile call, since it's not needed.&amp;nbsp;&amp;nbsp; Then in each mxd you find, you'll have to iterate through the layers and tables, get the workspaces of the data sources, and replace one by one with replaceWorkspaces.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If it were me, I wouldn't change the path without spaces to one with spaces.&amp;nbsp; That can cause trouble if you are not careful, both in scripts and toolbox tools.&amp;nbsp; So I'd drop this entire project, unless there's a really, really good reason for doing it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good luck,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Aug 2012 21:11:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402133#M31667</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2012-08-04T21:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Python issue finding and replacing file path's for mxd's in a loop</title>
      <link>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402134#M31668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;since the folder structure will remain the same other than the root folder name changing&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;If this is the case, then maybe the better approach is to make sure your MXDs are all using relative paths instead of going to the labor of changing the filepath for everything.&amp;nbsp; To do this with python you might be able to use the relativePaths property on the MapDocument, looping through each mxd and setting that value to true. You have to be careful though that the relationship of that map to all data sources is indeed static or the relative paths capability won't work for you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So this could be as easy as something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;maplist = ['mxd_a','mxd_b'... ] #a list of map documents, however you compile this
for map in maplist:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(map)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.relativePath = 'True'&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing, you probably want to do this before the root folders have been changed, as otherwise I don't think this approach will work if the links have already been broken.&amp;nbsp; I'd also ditto the other respondent's remark about changing workspace names to include spaces; generally this is a bad idea as there seem to be a lot of cases where arcGIS tools still rely on technologies that don't like spaces or long (&amp;gt; 13 characters or so) in file or path names.&amp;nbsp; Windows maybe ok with it these things, but ArcGIS not so much.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:19:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-issue-finding-and-replacing-file-path-s-for/m-p/402134#M31668</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-11T18:19:59Z</dc:date>
    </item>
  </channel>
</rss>

