[GIS] Saving currently opened mxd and changes using ArcPy

arcgis-desktoparcpy

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.

Best Answer

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")
Related Question