portable way of locating an executable (like which)

G

Gelonida N

I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.
If not, is there a portable way of splitting the environment variable PATH?

Thanks for any sugestions
 
N

Nobody

I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Note that "which" attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The "type" built-in command performs a similar
function to "which" but using the shell's semantics. On some systems,
the default configuration may alias "which" to "type".

On Windows, there's a host of different "execute program" interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).
 
T

Tarek Ziadé

Note that "which" attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The "type" built-in command performs a similar
function to "which" but using the shell's semantics. On some systems,
the default configuration may alias "which" to "type".

On Windows, there's a host of different "execute program" interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).
You can also look at shutil.which

http://hg.python.org/cpython/file/aa153b827d17/Lib/shutil.py#l974


Mmmm I wonder why it's removed in the last revs..
 
R

Ramchandra Apte

I'd like to implement the equivalent functionality of the unix command

/usr/bin/which



The function should work under Linux and under windows.



Did anybody already implement such a function.

If not, is there a portable way of splitting the environment variable PATH?



Thanks for any sugestions

shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
You can copy the code to support older Python versions.
 
R

Ramchandra Apte

I'd like to implement the equivalent functionality of the unix command

/usr/bin/which



The function should work under Linux and under windows.



Did anybody already implement such a function.

If not, is there a portable way of splitting the environment variable PATH?



Thanks for any sugestions

shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
You can copy the code to support older Python versions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top