我可以在运行时使用 python 打开应用程序吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14831716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I open an application using python during run time
提问by Anurag-Sharma
I was wondering if i could open any kind of application in Python during run time ?
我想知道我是否可以在运行时用 Python 打开任何类型的应用程序?
采纳答案by eandersson
Assuming that you are using Windows you would use one of the following commands like this.
假设您使用的是 Windows,您将使用以下命令之一。
import subprocess
subprocess.call('C:\myprogram.exe')
import os
os.startfile('C:\myprogram.exe')
回答by Christian Kiewiet
Try having a look at subprocess.callhttp://docs.python.org/2/library/subprocess.html#using-the-subprocess-module
尝试查看subprocess.callhttp://docs.python.org/2/library/subprocess.html#using-the-subprocess-module
回答by eandersson
Use the this code : -
使用此代码:-
import subprocess
subprocess.call('drive:\programe.exe')
回答by Rahul Sapparapu
Try this :
尝试这个 :
import os
import subprocess
command = r"C:\Users\Name\Desktop\file_name.exe"
os.system(command)
#subprocess.Popen(command)
回答by Muhammad Waqas Dilawar
Of course you can. Just import import subprocessand invoke subprocess.call('applicaitonName').
当然可以。只需导入import subprocess和调用subprocess.call('applicaitonName').
For example you want to open VS Code in Ubuntu:
例如你想在Ubuntu 中打开 VS Code :
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
如果您需要更多信息,此行也可用于打开应用程序,例如因为我想捕获错误所以我使用了stderr
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
回答by Kevin Sabbe
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.
使用 system 你还可以利用 open 功能(特别是如果你使用的是 mac os/unix 环境。当你面临权限问题时很有用。
import os
path = "/Applications/Safari.app"
os.system(f"open {path}")

