subprocess problem

Cameron Simpson cs at zip.com.au
Thu Feb 9 17:53:32 EST 2017


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.

>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.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list