<?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: Iterating through MXDs to change datasource shapefile to gbd in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415483#M70467</link>
    <description>&lt;P&gt;I added the mxd.save() just now just above the del mxd. I received this error: mxd.save()&lt;BR /&gt;AttributeError: 'unicode' object has no attribute 'save'&lt;/P&gt;</description>
    <pubDate>Thu, 25 Apr 2024 18:52:47 GMT</pubDate>
    <dc:creator>kaww02</dc:creator>
    <dc:date>2024-04-25T18:52:47Z</dc:date>
    <item>
      <title>Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415467#M70464</link>
      <description>&lt;P&gt;I am trying my hand at python for the first time. Using 2.7.18 Idle to check for errors (b/c I don't understand visual studio yet)&amp;nbsp; I am wanting to go through all MXDs in a folder, find the layer Backup (shapefile) and change its source to a different folder and a GDB with another name, then update the TOC layer name.&amp;nbsp;I don't get errors, but the script does not do anything.&amp;nbsp; All my printed statements print but nothing seems to be working.&amp;nbsp; Any suggestions for this newbie?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy
from arcpy import env

# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs in the workspace
for mxd in mxdList:
    mxdpath = env.workspace + "\\" + mxd
    print (mxd + "Is being processed")
    
    mxdNext = arcpy.mapping.MapDocument(mxdpath)

for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
    for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
        if lyr == "Backup":
           Lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
                                  r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
        if lyr.name == "Backup":
            lyr.name = "Final"
print("lyr.name")      


arcpy.RefreshTOC()
del mxd
    
print("Data sources updated for all MXDs.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:34:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415467#M70464</guid>
      <dc:creator>kaww02</dc:creator>
      <dc:date>2024-04-25T18:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415472#M70465</link>
      <description>&lt;P&gt;&amp;gt;"FILEGDB_WROKSPACE"&lt;/P&gt;&lt;P&gt;This might be the issue? Although tbh I feel like it'd throw an actual error at you.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:39:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415472#M70465</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-04-25T18:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415474#M70466</link>
      <description>&lt;P&gt;After making changes to your mxd via Python, you need to save those changes with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;mxd.save()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:42:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415474#M70466</guid>
      <dc:creator>Tom_Laue</dc:creator>
      <dc:date>2024-04-25T18:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415483#M70467</link>
      <description>&lt;P&gt;I added the mxd.save() just now just above the del mxd. I received this error: mxd.save()&lt;BR /&gt;AttributeError: 'unicode' object has no attribute 'save'&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:52:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415483#M70467</guid>
      <dc:creator>kaww02</dc:creator>
      <dc:date>2024-04-25T18:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415485#M70468</link>
      <description>&lt;P&gt;good catch. I corrected that. Still no error or changes made.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:53:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415485#M70468</guid>
      <dc:creator>kaww02</dc:creator>
      <dc:date>2024-04-25T18:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415489#M70469</link>
      <description>&lt;P&gt;Try &lt;STRONG&gt;mxdNext.save()&lt;/STRONG&gt; instead.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 18:55:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415489#M70469</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-04-25T18:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415497#M70470</link>
      <description>&lt;P&gt;line 18 you are trying to replaceDataSource on variable "Lyr", but your layer variable is set to lower case "lyr".&lt;/P&gt;&lt;P&gt;Is it just this capitalization typo that is causing it?&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:02:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415497#M70470</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2024-04-25T19:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415503#M70471</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;&amp;nbsp;is correct.&lt;BR /&gt;&lt;BR /&gt;If you're ever curious what a variable is you can type:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;print(type(mxd))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Also, the df part need to be tabbed over:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy import env

# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs in the workspace
for mxd in mxdList:
    mxdpath = env.workspace + "\\" + mxd
    print (mxd + "Is being processed")
    
    mxdNext = arcpy.mapping.MapDocument(mxdpath)

    for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
        for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
            if lyr == "Backup":
               lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
                                      r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
            if lyr.name == "Backup":
                lyr.name = "Final"
    print("lyr.name")      

    mxdNext.save()
    arcpy.RefreshTOC()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Otherwise it's only going to run on the last MXD in the folder, not all the MXDs.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:10:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415503#M70471</guid>
      <dc:creator>Tom_Laue</dc:creator>
      <dc:date>2024-04-25T19:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415511#M70472</link>
      <description>&lt;P&gt;Okay fixed the Lyr to lyr- based on all the changes now I get one map whose time stamp in file Explorer appears to have done something but when I open it, it is still the same. Also, if I open any map and try and save it under the same name/folder etc it says I don't have permissions for my folder to save or it is too full neither one of which is the case.&amp;nbsp; Thanks for all the help. I am sure I have a few errors causing all this. I thought putting print statements in would help me determine where it gets hung up but that does not seem to help.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:12:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415511#M70472</guid>
      <dc:creator>kaww02</dc:creator>
      <dc:date>2024-04-25T19:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415515#M70473</link>
      <description>&lt;P&gt;Python IDLE is probably putting a lock on those mxds.&lt;BR /&gt;&lt;BR /&gt;If you close the Python results Window, you should be able to edit in ArcMap.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:15:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415515#M70473</guid>
      <dc:creator>Tom_Laue</dc:creator>
      <dc:date>2024-04-25T19:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415520#M70474</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/299157"&gt;@Tom_Laue&lt;/a&gt;&amp;nbsp; I went back and indented the df and a few other tweaks. I got it to work now! Thanks to&amp;nbsp;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/15530"&gt;@RhettZufelt&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;&amp;nbsp; as well!&amp;nbsp; Hope I got everyone.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:22:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415520#M70474</guid>
      <dc:creator>kaww02</dc:creator>
      <dc:date>2024-04-25T19:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through MXDs to change datasource shapefile to gbd</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415525#M70475</link>
      <description>&lt;P&gt;Awesome&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":party_popper:"&gt;🎉&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Don't get too frustrated as it gets easier the more you use Arcpy.&lt;BR /&gt;The more code you write, the more you can "steal" ideas from your other scripts, or ESRI samples or GIS Stack Exchange, etc.&lt;BR /&gt;&lt;BR /&gt;Even ChatGPT sometimes can produce usable Esri Python code!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 19:31:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-mxds-to-change-datasource/m-p/1415525#M70475</guid>
      <dc:creator>Tom_Laue</dc:creator>
      <dc:date>2024-04-25T19:31:01Z</dc:date>
    </item>
  </channel>
</rss>

