import arcpy arcpy.env.workspace = "c:/arcgis/" # Using a relative path mxd = arcpy.mapping.MapDocument('maps/template.mxd') #works mxd.save() #fails -> "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." mxd.saveACopy('template_copy.mxd')
import arcpy, os, sys relPath = os.path.dirname(sys.argv[0]) mxd = arcpy.mapping.MapDocument(relPath + r"\project.mxd")
FYI I just tested and this works.import os import arcpy os.chdir("C:/GIS/test") arcpy.env.workspace = "C:/GIS/Test" mxd = arcpy.mapping.MapDocument("point_2_line.mxd") mxd.saveACopy("ortho_test_rename/testing3.mxd")
import os import arcpy os.chdir("C:/GIS/test") arcpy.env.workspace = "C:/GIS/Test" mxd = arcpy.mapping.MapDocument("point_2_line.mxd") mxd.saveACopy("ortho_test_rename/testing3.mxd")
import os import arcpy arcpy.env.workspace = r"C:\GIS" mxd = arcpy.mapping.MapDocument(r"Test\point_2_line.mxd") mxd.saveACopy(r"Test\testing4.mxd")
I'd also recommend removing the trailing slashes at the end of your directory strings.
arcpy.mapping.MapDocument should immediately return an error when a relative path is used.
Traceback (most recent call last): File "C:\GIS\Python\test_mxd_relative_path.py", line 7, in <module> mxd = arcpy.mapping.MapDocument(r"Test\point_2_line.mxd") File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 443, in __init__ assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename") AssertionError: Invalid MXD filename.
import os import arcpy os.chdir("C:/GIS") arcpy.env.workspace = r"C:\GIS" mxd = arcpy.mapping.MapDocument(r"Test\point_2_line.mxd") mxd.saveACopy(r"Test\testing4.mxd")
Traceback (most recent call last): File "C:\GIS\Python\test_mxd_relative_path.py", line 9, in <module> mxd.saveACopy(r"Test\testing4.mxd") File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\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.
Your chdir is what is doing it.
You are setting the relative path via python not the arcpy module.
FYI I just tested and this works.
import os import arcpy arcpy.env.workspace = "C:/arcgis" mxd = arcpy.mapping.MapDocument("maps/template2.mxd") mxd.saveACopy("maps/testing7.mxd")
''' c:/arcgis/ contents: template.mxd maps/template2.mxd ''' import arcpy, os os.chdir("c:/arcgis/") arcpy.env.workspace = "c:/arcgis/" # EXAMPLE 1 #use full path mxd = arcpy.mapping.MapDocument('c:/arcgis/maps/template2.mxd') mxd.saveACopy('template2_full_path_copy.mxd') #SUCCESSFULL #Need to reset env path os.chdir("c:/arcgis/") arcpy.env.workspace = "c:/arcgis/" # EXAMPLE 2 #use relative path but local source mxd = arcpy.mapping.MapDocument('template.mxd') mxd.saveACopy('template_relative_copy.mxd') #SUCCESSFULL # EXAMPLE 3 # Using a relative path, not local directory source mxd = arcpy.mapping.MapDocument('maps/template2.mxd') mxd.saveACopy('template2_copy_fails.mxd') #FAILS -> "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."
That being said, in this instance I cannot replicate this behaviour you are seeing. I have tried in an ArcGIS session and through IDLE. I get an error at mxd object creation with an invalid MXD filename error. What Service Pack are you using?
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\arcgis\maps\template.mxd") mxd.save()
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.