Find the path of a shell command

Cameron Simpson cs at cskk.id.au
Wed Oct 12 17:43:18 EDT 2022


On 12Oct2022 17:49, Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>Às 05:00 de 12/10/22, Paulo da Silva escreveu:
>I think that the the suggestion of searching the PATH env seems the 
>best.

I just want to note that you want to not just check for existence of the 
path, but that it is executable (permissionwise). You want to use `X_OK` 
with the `os.access` function:
https://docs.python.org/3/library/os.html#os.access

Also notice that the `shutil` module has a `which()` function:
https://docs.python.org/3/library/shutil.html#shutil.which

>Another thing that I thought of is that of the 'which', but, to avoid 
>the mentioned recurrent problem of not knowing where 'which' is I 
>would use 'type' instead.

`which` will almost always be in `/usr/bin` (or `/bin` on some systems I 
suppose). But do you need to know where `which` is? If you're invoking 
it instead of search `$PATH`, the normal executtion stuff itself search 
`$PATH`, an will find `which` for you :-)

>'type' is a bash (sh?) command.

Yeah. Bash, ksh etc have `type`. It is a builtin, shich can see if a 
command word you give it is a shell alias or function or builtin or 
externally executed command. As you'd imagine, an external executable 
like `which` has no access to the internals of your shell, and can only 
look for executables.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list