Select to view content in your preferred language

Arcpy command to start MXD

5161
6
04-02-2015 05:38 AM
AndrewTuleya2
Emerging Contributor

Hello,

I have a script that creates a series of maps via a python script. After the script runs the automation (via a batch file), i want to put in a command to actually launch/open the MXD files. I know I have used this syntax in the past, but I can't seem to locate it. Does anyone know what command to use for open an mxd?

0 Kudos
6 Replies
NathanialStory1
Occasional Contributor

Hi Andrew,

As far as an arcpy option, I am not sure. But I just tried this script and it worked for me within the python window in arcmap.

import os

os.startfile("Path to your mxd")

I think it emulates a double click event and opens pretty much any file you give it as far as I have seen.

AndrewTuleya2
Emerging Contributor

Thanks this is what I was looking for! However, now i am getting an error message saying "Coercing to Unicode: need string or buffer, Map document found." Any idea what this means?

0 Kudos
NathanialStory1
Occasional Contributor

That's a good question. Are you typing in the full path or giving it a variable with the path stored in it?

when I googled your error message I came across a similar stack exchange question here. I am by no means an expert with python but it looks like it might be expecting a string and instead getting a list or something.

AndrewTuleya2
Emerging Contributor

AH! I just tried os.startfile() but replaced the variable in () with a hardcoded full path to another MXD and it worked fine! I might have to do some switching and play with the concatenation of a new variable due to the fact that my script automatically creates project folders and geodatabases unique to a project ID #. But this is a great start, thanks.

0 Kudos
BlakeTerhune
MVP Regular Contributor

I try to use the r string prefix with path names so it escapes the backslashes.

mypath = r"C:\temp\myfile.mxd"

Or you can construct file paths with os.path

mydrive = "c:"
myfolder = "temp"
myfile = "mapdoc.mxd"
mypath = os.path.join(mydrive, myfolder, myfile)
AdamCox1
Deactivated User

Yes, os.path.join is great, and here's just one more option:

   mypath = r"C:\{0}\{1}.mxd".format(myfolder, myfilename)