subprocess problem

Wildman best_lay at yahoo.com
Thu Feb 9 19:05:57 EST 2017


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

> On 09Feb2017 11:59, Wildman <best_lay at 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 <cs at 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...



More information about the Python-list mailing list