Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29577 > unrolled thread

portable way of locating an executable (like which)

Started byGelonida N <gelonida@gmail.com>
First post2012-09-20 23:06 +0200
Last post2012-09-22 20:55 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  portable way of locating an executable (like which) Gelonida N <gelonida@gmail.com> - 2012-09-20 23:06 +0200
    Re: portable way of locating an executable (like which) Nobody <nobody@nowhere.com> - 2012-09-21 00:59 +0100
      Re: portable way of locating an executable (like which) Tarek Ziadé <tarek@ziade.org> - 2012-09-21 10:12 +0200
    Re: portable way of locating an executable (like which) Ramchandra Apte <maniandram01@gmail.com> - 2012-09-22 20:55 -0700
    Re: portable way of locating an executable (like which) Ramchandra Apte <maniandram01@gmail.com> - 2012-09-22 20:55 -0700

#29577 — portable way of locating an executable (like which)

FromGelonida N <gelonida@gmail.com>
Date2012-09-20 23:06 +0200
Subjectportable way of locating an executable (like which)
Message-ID<mailman.973.1348175220.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#29588

FromNobody <nobody@nowhere.com>
Date2012-09-21 00:59 +0100
Message-ID<pan.2012.09.20.23.59.52.522000@nowhere.com>
In reply to#29577
On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:

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

[toc] | [prev] | [next] | [standalone]


#29605

FromTarek Ziadé <tarek@ziade.org>
Date2012-09-21 10:12 +0200
Message-ID<mailman.991.1348215490.27098.python-list@python.org>
In reply to#29588
On 9/21/12 1:59 AM, Nobody wrote:
> On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:
>
>> 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).
>
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..

[toc] | [prev] | [next] | [standalone]


#29781

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-22 20:55 -0700
Message-ID<2a9605c0-7d6d-4518-b6ef-387a1daac418@googlegroups.com>
In reply to#29577
On Friday, 21 September 2012 02:37:01 UTC+5:30, gelonida  wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#29782

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-22 20:55 -0700
Message-ID<mailman.1098.1348372549.27098.python-list@python.org>
In reply to#29577
On Friday, 21 September 2012 02:37:01 UTC+5:30, gelonida  wrote:
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web