<?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 MapDocument.saveACopy() Error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/mapdocument-saveacopy-error/m-p/409397#M32287</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've getting an error with the saveACopy function of an arcpy MapDocument, it will not let me saveACopy of a document I created using saveACopy then later renamed back to the original documents name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my workflow, looping through a folder full of MXDs:&lt;/SPAN&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;Open original document&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Do some work on it&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Save a copy with new name&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Close all documents&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;copy the original document to an archive folder (only copying those documents that had work done to them successfully)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;delete the original document (in the original folder)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;rename the saved copy to the original documents name (in the original folder)&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;SPAN&gt;This works fine the first time it is run on a folder, if it is run a second time on the same folder (which now has some new documents), it will fail to saveACopy for all documents that were modified the first time around.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This sample function illustrates the problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os, gc
def Test():
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test mode
&amp;nbsp;&amp;nbsp;&amp;nbsp; justSaveAs = True


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test MXD
&amp;nbsp;&amp;nbsp;&amp;nbsp; originalDoc = r"C:\Users\USERNAME\Documents\ArcGIS\TestData\Test.mxd"


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a new MXD Name...
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseFldr = os.path.dirname(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseName = os.path.basename(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; newDoc = os.path.join(baseFldr,"temp123_" + baseName)


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Open the original Document, and save a copy
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Close the document
&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd
&amp;nbsp;&amp;nbsp;&amp;nbsp; gc.collect()


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete the original and rename the new file the same as the original
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(newDoc,originalDoc)


&amp;nbsp;&amp;nbsp;&amp;nbsp; if justSaveAs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Reopen the document and try and save as again -- FAILS
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Reopen the document, save, then save as -- WORKS!?
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It does work ok if you open the new version of a document and do a regular save before atempting to saveACopy. Also works by opening the new version of the document before renaming them and do saveACopy back to the original name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However both of these solutions require reopening the document which can take some time for large maps, and there is no way to tell before hand if the document will suffer from the saveACopy issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This issue only affects arcpy, opening the documents in ArcMap and doing save/save as/save a copy works fine for all MXDs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas on how to fix this?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 03 May 2013 14:18:43 GMT</pubDate>
    <dc:creator>AndrewEgleton</dc:creator>
    <dc:date>2013-05-03T14:18:43Z</dc:date>
    <item>
      <title>MapDocument.saveACopy() Error</title>
      <link>https://community.esri.com/t5/python-questions/mapdocument-saveacopy-error/m-p/409397#M32287</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've getting an error with the saveACopy function of an arcpy MapDocument, it will not let me saveACopy of a document I created using saveACopy then later renamed back to the original documents name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my workflow, looping through a folder full of MXDs:&lt;/SPAN&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;Open original document&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Do some work on it&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Save a copy with new name&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Close all documents&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;copy the original document to an archive folder (only copying those documents that had work done to them successfully)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;delete the original document (in the original folder)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;rename the saved copy to the original documents name (in the original folder)&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;SPAN&gt;This works fine the first time it is run on a folder, if it is run a second time on the same folder (which now has some new documents), it will fail to saveACopy for all documents that were modified the first time around.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This sample function illustrates the problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os, gc
def Test():
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test mode
&amp;nbsp;&amp;nbsp;&amp;nbsp; justSaveAs = True


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test MXD
&amp;nbsp;&amp;nbsp;&amp;nbsp; originalDoc = r"C:\Users\USERNAME\Documents\ArcGIS\TestData\Test.mxd"


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a new MXD Name...
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseFldr = os.path.dirname(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseName = os.path.basename(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; newDoc = os.path.join(baseFldr,"temp123_" + baseName)


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Open the original Document, and save a copy
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Close the document
&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd
&amp;nbsp;&amp;nbsp;&amp;nbsp; gc.collect()


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete the original and rename the new file the same as the original
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(newDoc,originalDoc)


&amp;nbsp;&amp;nbsp;&amp;nbsp; if justSaveAs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Reopen the document and try and save as again -- FAILS
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Reopen the document, save, then save as -- WORKS!?
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It does work ok if you open the new version of a document and do a regular save before atempting to saveACopy. Also works by opening the new version of the document before renaming them and do saveACopy back to the original name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However both of these solutions require reopening the document which can take some time for large maps, and there is no way to tell before hand if the document will suffer from the saveACopy issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This issue only affects arcpy, opening the documents in ArcMap and doing save/save as/save a copy works fine for all MXDs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas on how to fix this?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 May 2013 14:18:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mapdocument-saveacopy-error/m-p/409397#M32287</guid>
      <dc:creator>AndrewEgleton</dc:creator>
      <dc:date>2013-05-03T14:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: MapDocument.saveACopy() Error</title>
      <link>https://community.esri.com/t5/python-questions/mapdocument-saveacopy-error/m-p/409398#M32288</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I ran your code, and sure enough, it runs fine the first time but fails after that.&amp;nbsp; Not only that, but if I try to open the mxd that has gone through the process, I can't save it in Arcmap.&amp;nbsp; All I can do is a Save a Copy.&amp;nbsp; At this point the mxd can't quite figure out what its name is and is for all practicable purposes corrupt.&amp;nbsp; Seems like a bug to me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That said, the code below will work repeatedly if you can work it into your process.&amp;nbsp; The issue was the filesystem calls os.rename and os.remove. So we just do it all with arcpy:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; originalDoc = r'C:\temp\Untitled.mxd'

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a new MXD Name...
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseFldr = os.path.dirname(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; baseName = os.path.basename(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; newDoc = os.path.join(baseFldr,"temp123_" + baseName)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Open the original Document, and save a copy
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(originalDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(newDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(originalDoc)&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = None
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will be slower than what you were trying to do if the mxd's are large, as you said. The basic issue is the mixing of filesystem calls and saveACopy.&amp;nbsp; Probably the fastest way to handle your work flow is eliminate the saveACopy calls completely. Something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
originalDoc = r'C:\temp\Untitled.mxd'

# Make a new MXD Name...
baseFldr = os.path.dirname(originalDoc)
baseName = os.path.basename(originalDoc)
old_doc = os.path.join(baseFldr,"orig_" + baseName)

# make a copy of the original with a new name
shutil.copy2(originalDoc, old_doc)

# Open the original Document, do the work, and set work_done flag
mxd = arcpy.mapping.MapDocument(originalDoc)

# do some work here if necessary, set flag depending on if
#anything was done or not. We'll set to true for demonstration
work_done = True 

if work_done:
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp; # move the original to an archive folder
&amp;nbsp;&amp;nbsp;&amp;nbsp; afol = r'C:\temp\Archive'
&amp;nbsp;&amp;nbsp;&amp;nbsp; dest = os.path.join(afol, baseName)
&amp;nbsp;&amp;nbsp;&amp;nbsp; shutil.move(old_doc, dest)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(old_doc)&amp;nbsp;&amp;nbsp;&amp;nbsp; 

mxd = None

&lt;/PRE&gt;&lt;BR /&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, 11 Dec 2021 18:36:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mapdocument-saveacopy-error/m-p/409398#M32288</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2021-12-11T18:36:30Z</dc:date>
    </item>
  </channel>
</rss>

