0

I have been searching but I am so confused. I so appologize if this has been answered before but I have looked and I am even more confused. All I want to do is run an executable file from a python script.

I know to open notepad.exe (for instance) you do the following.

#opens notepad in windows
import os
print os.system('notepad.exe')

but what if I want to run something specific in a directory

How can I effectively run something like this (this is obviously going to fail)

#opens program in windows
import os
print os.system('c:\files\program.exe')

the more I read about it, the more confused I get.

I have been reading about sys.executable as well as surprocess but its confusing me more than helping. Could someone clarify how this can be done? An example, perhaps to run a "program.exe" file?

6
  • 1
    Fist thing is to make your path to the executable valid. Using backslashes in path is readable in code, but the real string in Python is using it as escacpe character. So you shall either use forward slashes (I recommend it even for Windows), or using "\\" instead of "\" Commented May 13, 2014 at 15:27
  • or the r literal flag ... Commented May 13, 2014 at 15:30
  • it's subprocess*, btw. that might clear up some of the confusion Commented May 13, 2014 at 15:30
  • @bernie what are you talking about? subprocess is another way (arguably better, although slightly more complicated) to accomplish what OP is asking Commented May 13, 2014 at 15:32
  • 1
    @JoranBeasley: OP misspelled Commented May 13, 2014 at 15:37

2 Answers 2

3

You can use os.system like that. Note that strings require proper escaping though, so you might need to escape those backslash characters. Alternatively, you can also use a raw string to make it work:

os.system(r'c:\files\program.exe')
Sign up to request clarification or add additional context in comments.

4 Comments

although to be honest python always understands linux style paths, and I would recommend that ... but meh +1
@JoranBeasley I personally prefer to use the system’s native path separators. That’s what os.path.join uses and why os.sep exists. Also you won’t end up mixing the separators when using it, for example, as arguments to programs that do not support any separator.
@poke the downside is if you store them somewhere that is accessed by various machines or hardcode them into the program or something ... windows slashes only work in windows(afaik ... I might be wrong) while linux paths work in python regardless of OS
@JoranBeasley Something called program.exe will rarely work on non-Windows machines though ;)
0

You can also use subprocess module https://docs.python.org/2/library/subprocess.html

import subprocess
subprocess.call('c:\files\program.exe')

1 Comment

If os.system fails with the path, it will fail with subprocess.call too.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.