Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Here is what I have so far...

import arcpy

import arcpy.mapping as map

mxd = map.MapDocument()

mxd.save()

And yes I know that map.MapDocument requires an argument, but I want it to automatically select the currently open mxd, and overwrite it. If I can't get it to select the currently open mxd I can use:

> import arcpy
> 
> import arcpy.mapping as map
> 
> mxd = map.MapDocument(C:\GIS\Untitled.mxd)
> 
> mxd.save()

But I would still like it to overwrite the mxd with the changes I just made to it in arcmap. Think of it like the save button except scripted. Any help is greatly appreciated.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Your path should look like this, using arcpy.mapping.MapDocument and surround your path with double quotes and double up your back slash:

mxd = arcpy.mapping.MapDocument("C:\\GIS\\Untitled.mxd")

or

Use string literal:

mxd = arcpy.mapping.MapDocument(r"c:\Temp\MXDs")

or

Add double quotes and change out backslash to forward slash:

mxd = arcpy.mapping.MapDocument("C:/GIS/Untitled.mxd")

If you only have one map document open you can define current map as:

mxd = arcpy.mapping.MapDocument("CURRENT")
share|improve this answer
1  
Can you set: mxd = arcpy.mapping.MapDocument("C:/GIS/Untitled.mxd"); open the mxd in arcmap; make changes; and save via mxd.save() ? –  gm70560 May 1 '13 at 16:56
    
Thanks bud, The "CURRENT" option solved my problem –  gomapping May 1 '13 at 17:26

(salam) Maybe you open the mxd file in arcmap and this file marked as open file and python can't overwrite the file. i think your error is

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

for solve this problem use saveACopy method and save it in another file.

import arcpy;
mxd = arcpy.mapping.MapDocument(r"D:\xTemp\sample.mxd");
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
mxd.saveACopy(r"d:\xTemp\name.mxd");
mxd.save();

Runtime error Traceback (most recent call last): File "", line 1, in File "e:\program files\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "e:\program files\arcgis\desktop10.1\arcpy\arcpy_mapping.py", line 801, in save return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True))) 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.

 mxd2=arcpy.mapping.MapDocument(r"D:\xTemp\name.mxd");
 mxd2.save(); // in here file save with no error
 del mxd2;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.