1

I have a script that saves a copy of the current .mxd to a location given to the script by a user. However I keep getting an error that says it failed to save the document.

I have writing rights in the folder and I have tried different locations as well, which doesn't seem to work either.

I don't seem to be able to find what I am missing, would anyone be able to tell me where I am going wrong? I am using ArcGIS 10.3.1.

Old code:

import arcpy

import arcpy.mapping as map

Project_number = arcpy.GetParameterAsText(0)
Folder_location = arcpy.GetParameterAsText(0)

mxd = arcpy.mapping.MapDocument("CURRENT")

mxd.saveACopy(r"{}\{}\{}.mxd".format(Folder_location, Project_number, Project_number))


del mxd

I have changed my code a bit with the help of the comments underneath. It works fine when I put a string for "Project_number" and "Folder_location". However when I set it as a parameter, it fails to save.

The parameters would be this:

Folder_location: U:\Example

Project_number: test

So, when I put these in the script as strings, they work. But when I set these as parameters to be chosen when the script starts (with the exact same data) it fails.

Any ideas?

New code:

import arcpy

import arcpy.mapping as map

import os

Project_number = arcpy.GetParameterAsText(0)
Folder_location = arcpy.GetParameterAsText(0)
Corrected = os.path.normcase(Folder_location)
MXD = ".mxd"
MXDName = Project_number + MXD
Pathname = os.path.join(Corrected, Project_number, MXDName)
CPath = os.path.normcase(Pathname)

mxd = arcpy.mapping.MapDocument("CURRENT")

mxd.saveACopy(CPath)

del mxd
7
  • 1
    What does print r"{}\{}\{}.mxd".format(Folder_location, Project_number, Project_number) output? Ok looking path+filename?
    – BERA
    Mar 18, 2020 at 14:01
  • I set Project_number as "test" and Folder_location as "U:\" for this test. It is supposed to print "U:\test\test", but it comes back as "test/test/test".
    – Remko
    Mar 18, 2020 at 14:07
  • Try using os.path.join instead of format to combine path and filename
    – BERA
    Mar 18, 2020 at 14:08
  • Python best practice is to use os.path.join to assemble paths. What does print(os.path.join(Folder_location, Project_number, "{}.mxd".format(Project_number))) return?
    – Vince
    Mar 18, 2020 at 14:11
  • The os.path.join option seems to be working, there is however one issue. The user will be able to select a folder, which is written away as a string as such: "U:\Examplefolder". The Project_number is then a folder after the "Examplefolder" aswel as the .mxd name. The "os.path.join" doesn't like the "\" in "U:\Examplefolder", is there a way to work around this? Seeing as the "U:\Examplefolder" is one string and Id prefer to keep it that way, so the user only has to select 1 item.
    – Remko
    Mar 18, 2020 at 14:29

1 Answer 1

0

The issue was solved by using os.path.join() as stated in the code below. The other issue I had with this code was a dumb copy and paste overlook.

import arcpy

import arcpy.mapping as map

import os

Project_number = arcpy.GetParameterAsText(0)
Folder_location = arcpy.GetParameterAsText(0)
Corrected = os.path.normcase(Folder_location)
MXD = ".mxd"
MXDName = Project_number + MXD
Pathname = os.path.join(Corrected, Project_number, MXDName)
CPath = os.path.normcase(Pathname)

mxd = arcpy.mapping.MapDocument("CURRENT")

mxd.saveACopy(CPath)

del mxd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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