【问题标题】:Check if a program is installed with Python检查程序是否安装了 Python
【发布时间】:2026-02-06 22:20:05
【问题描述】:

我的代码要求安装 UFW(简单防火墙)。该程序只能在基于 Linux 的操作系统上运行。如果发现安装了 UFW,我将如何让 Python 返回一个值?

编辑:我不想捕获输出,而是想查看文件夹(在默认安装路径中)是否存在。

【问题讨论】:

标签: python-3.x linux


【解决方案1】:

请注意,这只是链接的编辑副本

  1. How to find if directory exists in Python
  2. Check if a program exists from a python script

希望对你有帮助

import subprocess
rc = subprocess.call(['which', 'ufw'])
if rc == 0:
    print('ufw installed!')
else:
    print('ufw missing in path!')

# To check whether path exists

if os.path.exists("/usr/sbin/ufw") :
  print("path exists")
else:
  print("path doesn't exist") 

【讨论】: