python - 在python中检测程序

标签 python linux wine

<分区>

Possible Duplicate:
Test if executable exists in Python?

有没有一个python函数可以让我检测电脑中是否安装了一个程序。我有一个运行 .exe 的程序,该部分适用于 Windows,但要在 Linux 中运行它,您需要 wine,所以我需要一种方法让 python 函数检测 wine。

最佳答案

您可以使用函数 os.get_exec_path() 获取在 PATH 环境变量中设置的目录列表。如果您要查找的可执行文件不存在于任何这些目录中,则可以正确假设该程序未安装。

确定是否安装了 Wine 的代码如下所示:

import os
winePath = None
for directory in os.get_exec_path():
    testWinePath = os.path.join(directory, "wine")
    if os.path.exists(testWinePath) and os.access(testWinePath, os.R_OK | os.X_OK):
        winePath = executablePath
        break

如果安装了 Wine,那么它的可执行文件 (wine) 的路径将在 winePath 变量中;如果未找到,winePath 将为 None。 该代码还检查文件是否具有正确的读取和执行权限。

os.get_exec_path() 从 Python 3.2 开始可用。在旧版本中,您可以改用 os.environ["PATH"].split(":")

关于python - 在python中检测程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360691/

相关文章:

java - 如何通过python在cgi网站上执行jar?

python - 如何通过post请求传递序列化的二进制数据

linux - 计算文件的CRC32校验和

c# - Linux Mono 中使用快速彩色文本框的 System.NullReferenceException

linux - 我如何将我的 Windows 程序编译成与 Wine 一起运行的单个 Linux 二进制文件?

python - 如何在Python多模块的覆盖范围中排除目录

python - _curses.error : addwstr() returned ERR on changing nlines to 1 on newwin method

regex - 将文件名中除扩展名之外的所有 '.' 重命名为 '_'

mysql - 从Windows连接到LINUX上的MYSQL数据库

CentOS 5.5 中 Wine 中的 .Net 框架