'which' equivalent function in Python

I need to setup environment by running which abc command. Is there a Python equivalent function of the which command? This is my code.

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。


I know this is an older question, but if you happen to be using Python 3.3+ you can use shutil.which(cmd) . You can find the documentation here. It has the advantage of being in the standard library.

An example would be like so:

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

(Similar question)

See the Twisted implementation: twisted.python.procutils.which

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

上一篇: 检测可执行文件是否在用户的PATH上

下一篇: '哪个'是Python中的等价函数