Launching .exe in python

56737
19
12-28-2010 11:30 AM
DanPajak
Occasional Contributor
I am trying to launch microsoft access.exe and have know errors but also no result with opening access. Does anyone know how to do this. I am using ArcDesktop ten with latest service pack and have python 2.6 installed. 


import os
os.spawn("c:\programs files\Microsoft office\office12\msaccess.exe")
Tags (2)
0 Kudos
19 Replies
LoganPugh
Occasional Contributor III
Two things:
1 - Backslashes are an escape character in Python. Either escape your backslashes with another backslash, put an "r" at the front of the string (just before the opening quote) to denote that it is a raw string, or replace the backslashes with forward slashes.

2 - Use the subprocess module instead, as it is intended to replace other methods of executing external programs.

e.g.

import subprocess
subprocess.Popen([r"c:\programs files\Microsoft office\office12\msaccess.exe"])
0 Kudos
DanPajak
Occasional Contributor
I used the code as in your example and still I get nothing to launch.   Is there anything at the begining or ending of the python script that I could be missing for this not to work.   This is a simple command and works if I put it in command line but nothing in python.
0 Kudos
LoganPugh
Occasional Contributor III
Check the path, I simply copied yours and I see a typo in what should be "program files". Using my own path to MSACCESS.exe works for me. It should be throwing an error if it can't find the file though.
0 Kudos
DanPajak
Occasional Contributor
I corrected the typo and still get nothing.  Know error is prompted when I run code in python idle.  When I run the code as a .py script it flashes but then access doesn't open.
0 Kudos
DanPajak
Occasional Contributor
i ran the code in python with in arcmap instead of idle and I get an error. 
>>> import subprocess subprocess.Popen([r"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE])
Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)
>>>

Any ideas on this one?  I know it is the correct path.
0 Kudos
LoganPugh
Occasional Contributor III
Don't forget the closing quote. Also the two commands should be on separate lines.
0 Kudos
DanPajak
Occasional Contributor
>>> import subprocess
... subprocess.Popen([r"c:\programs files\Microsoft office\office12\msaccess.exe"])
... 
Runtime error <type 'exceptions.WindowsError'>: [Error 3] The system cannot find the path specified

I get this error.  Which is odd because I know the path is correct.
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Jeremy,

You've garbled the path again. And others may know for sure, but wouldn't the raw string be case sensitive?

Needs to be as entered as in your 12/30 6:39 post (but with the extra closing ").

Stuart
0 Kudos
LoganPugh
Occasional Contributor III
Windows' file system is case-insensitive by default so it does not matter what case you give to Python. On UNIX-like OS's it may be a different story.
0 Kudos