Select to view content in your preferred language

How to save opened mxd's using ArcPy?

10154
12
03-25-2015 04:14 AM
Yaron_YosefCohen
Frequent Contributor

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

probably a path issues... for this ancient thread

see Filenames and file paths in Python

>>> mxd = "text.mxd"
>>> path = r"D:\PROJECTS\bat_galim\gis\\" + mxd
>>> path
'D:\\PROJECTS\\bat_galim\\gis\\\\text.mxd'    # hmmmmmmm
>>> path2 = r"D:\PROJECTS\bat_galim\gis" + "\\" + mxd
>>> path2
'D:\\PROJECTS\\bat_galim\\gis\\text.mxd'
Yaron_YosefCohen
Frequent Contributor

when i try to save opened MXD's i get an error. And my path is correct, so i don't see any reason to change it

0 Kudos
DanPatterson_Retired
MVP Emeritus

and you confirmed that from my example... throw in a print statement and see what it returns and as James says, set overwriting to true

0 Kudos
JamesCrandall
MVP Frequent Contributor

Maybe try to set the workspace Overwrite property?

arcpy.env.overwriteOutput = True

Yaron_YosefCohen
Frequent Contributor

I enter this line in:

mapdoc.save()
arcpy.env.overwriteOutput = True

And got this 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.
0 Kudos
DanPatterson_Retired
MVP Emeritus

it needs the full path to the mapdoc, or has to be 'current'.  If you don't have write permission for whatever reasons it throws the message.

MapDocument—Help | ArcGIS for Desktop

0 Kudos
JamesCrandall
MVP Frequent Contributor

I'm not sure where you placed the line (it looks like you put it in the for loop?), but it should probably be located just after you set the workspace:

env.workspace = r"D:\PROJECTS\bat_galim\gis" 
env.overwriteOutput = True  
0 Kudos
Yaron_YosefCohen
Frequent Contributor

i added it to this place- it still throws the same message.  Maybe i wasn't clear in my question- the MXD's still open while i run this code. 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 this what i did with this new row that James send me-there still an error.

0 Kudos
JamesCrandall
MVP Frequent Contributor

Your code works fine for me.  Double-check the folder and file permissions.

0 Kudos