在Python中,是否有一种可移植且简单的方法来测试可执行程序是否存在?

我说的简单是指像which命令这样完美的命令。我不想手动搜索PATH或涉及尝试与Popen & al执行它,看看它是否失败(这就是我现在做的,但想象它是launchmissiles)


当前回答

对于python 3.3及更高版本:

import shutil

command = 'ls'
shutil.which(command) is not None

简·菲利普·格尔克(Jan-Philip Gehrcke)的一句话:

cmd_exists = lambda x: shutil.which(x) is not None

作为一个定义:

def cmd_exists(cmd):
    return shutil.which(cmd) is not None

对于python 3.2及更早版本:

my_command = 'ls'
any(
    (
        os.access(os.path.join(path, my_command), os.X_OK) 
        and os.path.isfile(os.path.join(path, my_command)
    )
    for path in os.environ["PATH"].split(os.pathsep)
)

这是Jay的回答中的一行代码,也是一个lambda func:

cmd_exists = lambda x: any((os.access(os.path.join(path, x), os.X_OK) and os.path.isfile(os.path.join(path, x))) for path in os.environ["PATH"].split(os.pathsep))
cmd_exists('ls')

最后,缩进为函数:

def cmd_exists(cmd, path=None):
    """ test if path contains an executable file with name
    """
    if path is None:
        path = os.environ["PATH"].split(os.pathsep)

    for prefix in path:
        filename = os.path.join(prefix, cmd)
        executable = os.access(filename, os.X_OK)
        is_not_directory = os.path.isfile(filename)
        if executable and is_not_directory:
            return True
    return False

其他回答

增加了windows支持

def which(program):
    path_ext = [""];
    ext_list = None

    if sys.platform == "win32":
        ext_list = [ext.lower() for ext in os.environ["PATHEXT"].split(";")]

    def is_exe(fpath):
        exe = os.path.isfile(fpath) and os.access(fpath, os.X_OK)
        # search for executable under windows
        if not exe:
            if ext_list:
                for ext in ext_list:
                    exe_path = "%s%s" % (fpath,ext)
                    if os.path.isfile(exe_path) and os.access(exe_path, os.X_OK):
                        path_ext[0] = ext
                        return True
                return False
        return exe

    fpath, fname = os.path.split(program)

    if fpath:
        if is_exe(program):
            return "%s%s" % (program, path_ext[0])
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return "%s%s" % (exe_file, path_ext[0])
    return None

对于python 3.3及更高版本:

import shutil

command = 'ls'
shutil.which(command) is not None

简·菲利普·格尔克(Jan-Philip Gehrcke)的一句话:

cmd_exists = lambda x: shutil.which(x) is not None

作为一个定义:

def cmd_exists(cmd):
    return shutil.which(cmd) is not None

对于python 3.2及更早版本:

my_command = 'ls'
any(
    (
        os.access(os.path.join(path, my_command), os.X_OK) 
        and os.path.isfile(os.path.join(path, my_command)
    )
    for path in os.environ["PATH"].split(os.pathsep)
)

这是Jay的回答中的一行代码,也是一个lambda func:

cmd_exists = lambda x: any((os.access(os.path.join(path, x), os.X_OK) and os.path.isfile(os.path.join(path, x))) for path in os.environ["PATH"].split(os.pathsep))
cmd_exists('ls')

最后,缩进为函数:

def cmd_exists(cmd, path=None):
    """ test if path contains an executable file with name
    """
    if path is None:
        path = os.environ["PATH"].split(os.pathsep)

    for prefix in path:
        filename = os.path.join(prefix, cmd)
        executable = os.access(filename, os.X_OK)
        is_not_directory = os.path.isfile(filename)
        if executable and is_not_directory:
            return True
    return False

您可以尝试名为“sh”(http://amoffat.github.io/sh/)的外部库。

import sh
print sh.which('ls')  # prints '/bin/ls' depending on your setup
print sh.which('xxx') # prints None

看到操作系统。路径模块提供一些有用的路径名函数。要检查现有文件是否可执行,请使用os。访问(路径,模式),使用操作系统。X_OK模式。

操作系统。X_OK 要包含在access()的模式参数中的值,以确定是否可以执行path。

编辑:建议的which()实现缺少一个线索-使用os.path.join()来构建完整的文件名。

因此,基本上您希望在挂载的文件系统中找到一个文件(不一定只在PATH目录中)并检查它是否可执行。这可以转化为以下计划:

枚举本地挂载的文件系统中的所有文件 用名称模式匹配结果 对于找到的每个文件,检查它是否可执行

我想说,以一种便携式的方式来做这件事将需要大量的计算能力和时间。这真的是你需要的吗?