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


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

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

Started byMark Lawrence <breamoreboy@yahoo.co.uk>
First post2012-09-21 00:46 +0100
Last post2012-09-21 00:46 +0100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: portable way of locating an executable (like which) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-21 00:46 +0100

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

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-21 00:46 +0100
SubjectRe: portable way of locating an executable (like which)
Message-ID<mailman.983.1348184805.27098.python-list@python.org>
On 21/09/2012 00:15, Gelonida N wrote:
> On 09/21/2012 12:04 AM, Jason Swails wrote:
>>
> Thanks a lot Jason,
>
>
>> I've used the following in programs I write:
>>
>> def which(program):
>>     def is_exe(fpath):
>>        return os.path.exists(fpath) and os.access(fpath, os.X_OK)
>>
>>     fpath, fname = os.path.split(program)
>>     if fpath:
>>        if is_exe(program):
>>           return program
>>     else:
>>        for path in os.getenv("PATH").split(os.pathsep):
>>           exe_file = os.path.join(path, program)
>>           if is_exe(exe_file):
>>              return exe_file
>>     return None
>>
>> IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac
>> OS X, but not sure about windows (since I don't know if PATH works the
>> same way there).
>
> I'll try it, the script looks reasonably portable (using os.pathsep)
> to really replicate which I had probably to add os.getenv('pathext')
> as Chris mentioned.
> However for my current use case this is not necessarily required.
>>
>> HTH,
>> Jason
>>
>>
>>
>
>

http://nedbatchelder.com/code/utilities/wh_py.html

-- 
Cheers.

Mark Lawrence.

[toc] | [standalone]


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


csiph-web