Hi,
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.
This is my workflow, looping through a folder full of MXDs:
This sample function illustrates the problem:
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.
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.
This issue only affects arcpy, opening the documents in ArcMap and doing save/save as/save a copy works fine for all MXDs.
Any ideas on how to fix this?
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.
This is my workflow, looping through a folder full of MXDs:
- Open original document
- Do some work on it
- Save a copy with new name
- Close all documents
- copy the original document to an archive folder (only copying those documents that had work done to them successfully)
- delete the original document (in the original folder)
- rename the saved copy to the original documents name (in the original folder)
This sample function illustrates the problem:
Code:
import arcpy, os, gc
def Test():
# Test mode
justSaveAs = True
# Test MXD
originalDoc = r"C:\Users\USERNAME\Documents\ArcGIS\TestData\Test.mxd"
# Make a new MXD Name...
baseFldr = os.path.dirname(originalDoc)
baseName = os.path.basename(originalDoc)
newDoc = os.path.join(baseFldr,"temp123_" + baseName)
# Open the original Document, and save a copy
mxd = arcpy.mapping.MapDocument(originalDoc)
mxd.saveACopy(newDoc)
# Close the document
del mxd
gc.collect()
# Delete the original and rename the new file the same as the original
os.remove(originalDoc)
os.rename(newDoc,originalDoc)
if justSaveAs:
# Reopen the document and try and save as again -- FAILS
mxd = arcpy.mapping.MapDocument(originalDoc)
mxd.saveACopy(newDoc)
else:
# Reopen the document, save, then save as -- WORKS!?
mxd = arcpy.mapping.MapDocument(originalDoc)
mxd.save()
mxd.saveACopy(newDoc)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.
This issue only affects arcpy, opening the documents in ArcMap and doing save/save as/save a copy works fine for all MXDs.
Any ideas on how to fix this?