python library call equivalent to `which' command

Tim Pinkawa timpinkawa at
Mon Jun 29 15:31:25 EDT 2009

On Mon, Jun 29, 2009 at 2:17 PM, Christian Heimes<lists at> wrote:
> "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.

I realize four lines of Python does not replicate the functionality of
which exactly. It was intended to give the original poster something
to start with.

I am curious about it being slow, though. Is there a faster way to get
the contents of a directory than os.listdir() or is there a faster way
to see if an element is in a list other than "x in y"? I believe
'which' will terminate once it finds any match, which mine does not,
but that can be fixed by adding a break after the print.

More information about the Python-list mailing list