Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   portable way of locating an executable (like which) (http://www.velocityreviews.com/forums/t952474-portable-way-of-locating-an-executable-like-which.html)

Gelonida N 09-20-2012 09:06 PM

portable way of locating an executable (like which)
 
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



Nobody 09-20-2012 11:59 PM

Re: portable way of locating an executable (like which)
 
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).


Tarek Ziadé 09-21-2012 08:12 AM

Re: portable way of locating an executable (like which)
 
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/aa...shutil.py#l974


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

Ramchandra Apte 09-23-2012 03:55 AM

Re: portable way of locating an executable (like which)
 
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/s...l#shutil.which
You can copy the code to support older Python versions.

Ramchandra Apte 09-23-2012 03:55 AM

Re: portable way of locating an executable (like which)
 
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/s...l#shutil.which
You can copy the code to support older Python versions.


All times are GMT. The time now is 11:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.