python library call equivalent to `which' command
python at mrabarnett.plus.com
Tue Jun 30 00:08:15 CEST 2009
Tim Pinkawa wrote:
> On Mon, Jun 29, 2009 at 2:17 PM, Christian Heimes<lists at cheimes.de> 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.
Have a look at os.path.isfile(), os.path.isdir() and os.path.islink().
More information about the Python-list