问题
I was wondering if i could open any kind of application in Python during runtime?
回答1:
Assuming that you are using Windows you would use one of the following commands like this.
subprocess.call
import subprocess
subprocess.call('C:\\myprogram.exe')
os.startfile
import os
os.startfile('C:\\myprogram.exe')
回答2:
Using system you can also take advantage of open function (especially if you are using mac os/unix environment. Can be useful when you are facing permission issue.
import os
path = "/Applications/Safari.app"
os.system(f"open {path}")
回答3:
Try having a look at subprocess.call http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module
回答4:
Use the this code : -
import subprocess
subprocess.call('drive:\\programe.exe')
回答5:
Try this :
import os
import subprocess
command = r"C:\Users\Name\Desktop\file_name.exe"
os.system(command)
#subprocess.Popen(command)
回答6:
Of course you can. Just import import subprocess and invoke subprocess.call('applicaitonName').
For example you want to open VS Code in Ubuntu:
import subprocess
cmd='code';
subprocess.call(cmd)
This line can be also used to open application, if you need to have more information, e.g. as I want to capture error so I used stderr
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
来源:https://stackoverflow.com/questions/14831716/can-i-open-an-application-from-a-script-during-runtime