简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM