python library call equivalent to `which' command

Christian Heimes lists at
Mon Jun 29 15:17:20 EDT 2009

Tim Pinkawa wrote:
> def which(file):
>     for path in os.environ["PATH"].split(":"):
>             if file in os.listdir(path):
>                     print "%s/%s" % (path, file)

"if file in os.list()" is slow and not correct. You have to check if the
file is either a real file or a symlink to a file and not a directory or
special. Then you have to verify that the file has the executable bit, too.


More information about the Python-list mailing list