subprocess problem

MRAB python at mrabarnett.plus.com
Thu Feb 9 19:36:37 EST 2017


On 2017-02-10 00:05, Wildman via Python-list wrote:
> 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.
>
The implication was that you don't need the True/False, just the 
string/None.




More information about the Python-list mailing list