On Fri, 10 Feb 2017 09:53:32 +1100, Cameron Simpson wrote:

> On 09Feb2017 11:59, Wildman <best_...@yahoo.com> wrote:
>>Here is a method I frequently use to replace the which
>>command. (air code)
>>
>>import os
>>pathlist = os.environ["PATH"].split(":")
>>
>>def which(target)
>>    for p in pathlist:
>>        fullpath = p + "/" + target
>>        if os.path.isfile(fullpath):
>>            return fullpath, True
>>    return "", False
> 
> Cleaner is to return the path if found, or None if not.

I don't see this as an important issue but you are
right.

>>target, found = which("pandoc")
>>if found:
>>    target will contain the full path to pandoc
>>else:
>>    pandoc was not found
> 
> You want to check for execute permssion too.

For my uses of the above code, it is unlikely that
the 'target' would not have the exec bit set.  But,
your suggestion is valid due to the fact that I
should not take anything for granted.

> Cheers,
> Cameron Simpson <c...@zip.com.au>

Corrected code:

def which(target)
    for p in pathlist:
        fullpath = p + "/" + target
        if os.path.isfile(fullpath) and os.access(fullpath, os.X_OK):
            return fullpath, True
    return None, False

Thanks.

-- 
<Wildman> GNU/Linux user #557453
Keyboard not detected! Press any key to continue...
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to