error with mxd.save/mxd.SaveACopy

10766
9
04-09-2012 07:18 AM
Michele
Occasional Contributor
I'm trying to use arcpy to save an .mxd and am having an issue. It doesn't matter if I use mxd.save or mxd.saveACopy I get the same error. I know it doesn't look like I am doing anything w/ it here, but it's just a portion of my code that I've pulled out into a separate script to test:

import arcpy
import arcpy.mapping as map

mxd = map.MapDocument(r"C:\MGIS\Finalproject2.mxd")
mxd.saveACopy(mxd)


I get the following error:

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\MGIS\geog485\FinalProject2\mxdtest.py", line 7, in <module>
    mxd.saveACopy(mxd)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 668, in saveACopy
    self._arc_object.saveACopy(file_name)
AttributeError: 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.


Space is not the issue, I have 60 GB of free space and I've checked the windows properties of the .mxd file. Read-only box is unchecked and I've "allowed" all permissions for all groups:

[ATTACH=CONFIG]13339[/ATTACH]

Does anyone have any idea how if this is a bug or how I can fix this?  It's probably user error but is really holding me up. I'm using PythonWin 2.6 ArcInfo 10.0 SP4.

Thanks in advance.
Tags (2)
0 Kudos
9 Replies
AndrewChapkowski
Esri Regular Contributor
You need to pass a path to the function, not the MapDocument object.


import arcpy
import arcpy.mapping as map

mxd = map.MapDocument(r"C:\MGIS\Finalproject2.mxd")
mxd.saveACopy(r"c:\MGIS\FINALPROJECT3.mxd")



See http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000000n000000 for more information.
0 Kudos
Michele
Occasional Contributor
Thank you for the reply Andrew.  So say I want to make a script tool and will need the mxd variable in the following format:

mxd = map.MapDocument(arcpy.GetParameterAsText(0)) #r"C:\MGIS\Finalproject2.mxd" as the parameter in the script tool


would I then need to define another variable, say mxd1 for the save:

mxd1 = arcpy.GetParameterAsText(1) #r"C:\MGIS\Finalproject2mxd1.mxd" as the parameter in the script tool
mxd.saveACopy(mxd1)


or is there a more efficient way of doing this?

Thanks in advance.
0 Kudos
AndrewChapkowski
Esri Regular Contributor
For your first variable, if you input is a string, then yes you could do it that way.  There is also an input called ArcMap Document that you might want to look at.

For the 2nd parameter, assuming it's of type string, then yes that's how it would work.  I would also perform some checks to ensure that the input is a valid format of .mxd, and that the file you are going to write to does not exist.
0 Kudos
Michele
Occasional Contributor
Just for clarification...there isn't a way to overwrite an .mxd using arcpy? I have to use 2 variables??
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Sure you can overwrite a map document. 

I'd set the environmental overwrite output parameter to true.  If you are currently accessing the map document, I'd use save().  That will save any changes in the current map document.

If you want to export changes to say an existing file, you can use either standard python like the shutil or os module to copy and remove files, or you can use the arcpy Delete() and Copy() in the data management toolbox. You can also try just using the SaveAs() function on the MapDocument object with the overwrite output parameter set to true.
0 Kudos
Michele
Occasional Contributor
I didn't include it in the code I posted originally (my script is pretty long), but I already have env.workspace defined as a gdb file path (actually it's not hardcoded, I'm using arcpy.GetParameterAsText since I am also creating a script tool).

env.workspace = r"C:\fp2.gdb"


so that I don't have to define like 5 more variables when I need to call them further down in my script and can just use "":

ex.
arcpy.AddField_management ("dec209data", "dec2009dataLayer")


The problem is that you cannot save an .mxd into a gdb so the location of my mxd is not within the env.workspace (it's in the same folder as the .gdb though - C:\fp2copy).  So I believe this is why it will not overwrite the mxd and I have to define another variable for an mxd if I want to be able to save the 4 layers that I am adding to it using arcpy.mapping module (as in my previous post using mxd1). 

Correct me if I am wrong, but the only way I would be able to overwrite my mxd is to change the env.workspace to the folder I am processing everything to/from (C:\fp2copy) and then define variables for all of the datasets currently being saved within the gdb (see attached) or will one of the other methods you suggested work (os module etc.)?  I'm trying to create the least amount of variables as possible for efficiency.

Thanks so much for your assistance!!
0 Kudos
AndrewChapkowski
Esri Regular Contributor
You can't save an mxd inside a file geodatabase, you need to save it in a folder.

To get the path of your workspace, try this:
import os

#... code
workPath = os.path.dirname(env.workspace)
print workPath
# do stuff
del workPath


I didn't include it in the code I posted originally (my script is pretty long), but I already have env.workspace defined as a gdb file path (actually it's not hardcoded, I'm using arcpy.GetParameterAsText since I am also creating a script tool).

env.workspace = r"C:\fp2.gdb"


so that I don't have to define like 5 more variables when I need to call them further down in my script and can just use "":

ex.
arcpy.AddField_management ("dec209data", "dec2009dataLayer")


The problem is that you cannot save an .mxd into a gdb so the location of my mxd is not within the env.workspace (it's in the same folder as the .gdb though - C:\fp2copy).  So I believe this is why it will not overwrite the mxd and I have to define another variable for an mxd if I want to be able to save the 4 layers that I am adding to it using arcpy.mapping module (as in my previous post using mxd1). 

Correct me if I am wrong, but the only way I would be able to overwrite my mxd is to change the env.workspace to the folder I am processing everything to/from (C:\fp2copy) and then define variables for all of the datasets currently being saved within the gdb (see attached) or will one of the other methods you suggested work (os module etc.)?  I'm trying to create the least amount of variables as possible for efficiency.

Thanks so much for your assistance!!
0 Kudos
Michele
Occasional Contributor
You can't save an mxd inside a file geodatabase, you need to save it in a folder.

To get the path of your workspace, try this:
import os

#... code
workPath = os.path.dirname(env.workspace)
print workPath
# do stuff
del workPath


That does allow me to define the path of the mxd to a folder, but what about the overwrite capability?

arcpy.env.overwriteOutput = True


My env.workspace is currently defined to the gdb, so it still won't allow me to overwrite the mxd.
0 Kudos
JulianaQuist
New Contributor II

This is just a simple thought, but...

Make sure that you do not have the script open in your IDE when you are trying to run in ArcMap and the other way around. This will cause locking/permissions problems like what you are seeing

0 Kudos