在Python中,是否有一种可移植且简单的方法来测试可执行程序是否存在?
我说的简单是指像which命令这样完美的命令。我不想手动搜索PATH或涉及尝试与Popen & al执行它,看看它是否失败(这就是我现在做的,但想象它是launchmissiles)
在Python中,是否有一种可移植且简单的方法来测试可执行程序是否存在?
我说的简单是指像which命令这样完美的命令。我不想手动搜索PATH或涉及尝试与Popen & al执行它,看看它是否失败(这就是我现在做的,但想象它是launchmissiles)
当前回答
这看起来很简单,在python2和python3中都可以使用
try: subprocess.check_output('which executable',shell=True)
except: sys.exit('ERROR: executable not found')
其他回答
一个重要的问题是“为什么需要测试可执行文件是否存在?”也许你不知道?: -)
最近我需要这个功能来启动PNG文件的查看器。我想迭代一些预定义的查看器,并运行第一个存在的查看器。幸运的是,我遇到了os.startfile。好多了!简单,可移植,并使用系统上的默认查看器:
>>> os.startfile('yourfile.png')
更新:关于操作系统我错了。开始文件是可移植的…这是Windows独有的。在Mac上,你必须运行open命令。Unix上的xdg_open。添加Mac和Unix对os.startfile的支持存在Python问题。
使用Python标准库中的shutil.which()。 电池包括!
在标准的Python发行版中有一个which.py脚本(例如在Windows的` \PythonXX\Tools\Scripts\which.py `)。
EDIT: which.py依赖于ls,因此它不是跨平台的。
增加了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
只要记住在windows上指定文件扩展名即可。否则,你必须使用PATHEXT环境变量为windows编写一个非常复杂的is_exe。您可能只想使用FindPath。
哦,你为什么还要费心搜索可执行文件呢?操作系统将作为popen调用的一部分为你做这件事,如果找不到可执行文件,将引发一个异常。您所需要做的就是为给定的操作系统捕获正确的异常。注意,在Windows上,subprocess。如果没有找到exe, Popen(exe, shell=True)将会静默失败。
将PATHEXT合并到上面的实现中(在Jay的回答中):
def which(program):
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK) and os.path.isfile(fpath)
def ext_candidates(fpath):
yield fpath
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
yield fpath + ext
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
for candidate in ext_candidates(exe_file):
if is_exe(candidate):
return candidate
return None