首页 > 解决方案 > 用于打开 ubuntu 应用程序的 Python 脚本

问题描述

如何编写python脚本在ubuntu中打开应用程序

我尝试了 os 模块,但它没有用

标签: pythonlinuxubuntu

解决方案


运行系统程序的两种常用方法是 os.system() 和 subprocess 模块。

os.system获取类似命令的终端作为参数,因此您可以像在终端中一样启动后台进程

>>> import os
>>> os.system("nautilus /tmp &")
0

如果要获取流程输出;

>>> import subprocess
>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT,
...     shell=True)
'ls: non_existent_file: No such file or directory\n'

推荐阅读