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


Groups > comp.lang.python > #29579

Re: portable way of locating an executable (like which)

References <k3g0h6$mtr$1@ger.gmane.org> <k3g2t5$ar2$1@ger.gmane.org>
Date 2012-09-21 08:21 +1000
Subject Re: portable way of locating an executable (like which)
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.977.1348179691.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 20/09/2012 22:06, 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.
>>
>> Did anybody already implement such a function.
>
> Searching found nothing obvious to me :(
>
>> If not, is there a portable way of splitting the environment variable
>> PATH?
> With os.sep ?

os.sep is the directory separator, but os.pathsep may be what you
want. Between that and os.getenv('path') you can at least get the
directories. Then on Windows, you also need to check out
os.getenv('pathext') and split _that_ on the semicolon, and try each
of those as a file extension. I'm not sure whether or not Windows will
add extensions from pathext if one is given on the command line - for
instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
basics are there.

Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: portable way of locating an executable (like which) Chris Angelico <rosuav@gmail.com> - 2012-09-21 08:21 +1000

csiph-web