我可以在运行时使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:35:03  来源:igfitidea点击:

Can I open an application using python during run time

pythonpython-3.xpython-2.7

提问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,您将使用以下命令之一。

subprocess.call

子进程调用

import subprocess
subprocess.call('C:\myprogram.exe')

os.startfile

操作系统启动文件

import os
os.startfile('C:\myprogram.exe')

回答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}")