How to save opened mxd's using ArcPy?

9596
12
03-25-2015 04:14 AM
Yaron_YosefCohen
Occasional Contributor II

Hi,

I work on several open mxd's and i try to save them at once with arcpy:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env

env.workspace = r"D:\PROJECTS\bat_galim\gis"
for mxd in arcpy.ListFiles("*.mxd"):
    print mxd 
    mapdoc = arcpy.mapping.MapDocument(r"D:\PROJECTS\bat_galim\gis\\" + mxd)
    mapdoc.save()
    print 'mapdoc.save'
    counter = counter + 1
del mxd

but i get en error:

IOError: MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

what wrong with my code?

Tags (2)
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

if the project is open and you are trying to save it while it is open, you probably don't have permissions to do so at the operating level.... either close the project and just run the script, or change the name of the mxd to 'current'

0 Kudos
JamesCrandall
MVP Frequent Contributor

my propose was to save me to click on "save" buttun in all the open MXD's by my self- i wanted the python will do it.

So you are running the script from an Add-In tool?  I don't think it is possible to execute this type of save method from a script running parallel to the mxd document you are trying to alter --- I'd think there will be 2 separate instances of python conflicting.

You will likely have to check to see if an .mxd referenced in your for loop is open and perform a saveAs instead.

0 Kudos
MarielaDel_Rio
New Contributor III

I agree, you cannot save or delete the mxd because it is opened. You stated they are all opened. I imagine you are running this from the python window within an arcMap session. In this case the session thinks those files are opened, they are not the current document. If you want to save the current document, then you have to locate the save command object, but it will only save the current document.

As James suggested, you could use a saveAs, and then use an OS command to force ArcMap windows to close. It is not clean.

0 Kudos