jabbyjim
jabbyjim

Reputation: 11

shell command in python to run a specific windows executable

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?

Upvotes: 0

Views: 72

Answers (2)

andmart
andmart

Reputation: 552

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

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

Upvotes: 0

poke
poke

Reputation: 387547

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')

Upvotes: 3

Related Questions