'哪个'是Python中的等价函数

我需要通过运行which abc命令来设置环境。 有which Python命令的等效函数? 这是我的代码。

cmd = ["which","abc"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
res = p.stdout.readlines()
if len(res) == 0: return False
return True

有distutils.spawn.find_executable。


我知道这是一个较老的问题,但是如果你碰巧使用Python 3.3+,你可以使用shutil.which(cmd) 。 你可以在这里找到文档。 它具有在标准库中的优点。

一个例子会是这样的:

>>> import shutil
>>> shutil.which("bash")
'/usr/bin/bash'

(类似问题)

请参阅Twisted实现:twisted.python.procutils.which

链接地址: http://www.djcxy.com/p/57061.html

上一篇: 'which' equivalent function in Python

下一篇: Check if a program exists from a Makefile