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


Groups > comp.lang.python > #29586

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(using': 0.07; 'fname': 0.09; 'portable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; '12:04': 0.16; 'reasonably': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:which': 0.16; 'wrote:': 0.17; 'windows': 0.19; 'required.': 0.22; "i've": 0.23; 'linux': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; '(since': 0.29; 'subject:like': 0.29; 'url:code': 0.29; 'probably': 0.29; 'from:addr:yahoo.co.uk': 0.32; 'mac': 0.32; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'jason': 0.35; 'path': 0.35; 'add': 0.36; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; "i'll": 0.36; 'subject: (': 0.36; 'subject:: ': 0.38; 'mark': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'necessarily': 0.63; 'gelonida': 0.84; 'replicate': 0.84; 'write:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: portable way of locating an executable (like which)
Date Fri, 21 Sep 2012 00:46:48 +0100
References <k3g0h6$mtr$1@ger.gmane.org> <CAEk9e3rfcvbrjVpTUmX9RdtmpExe_owxfrEwJqDREzfUmNGR0w@mail.gmail.com> <k3g81l$i70$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-92-24-218-20.ppp.as43234.net
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
In-Reply-To <k3g81l$i70$1@ger.gmane.org>
X-Antivirus avast! (VPS 120920-0, 20/09/2012), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.983.1348184805.27098.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348184805 news.xs4all.nl 6879 [2001:888:2000:d::a6]:43803
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29586

Show key headers only | View raw


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.

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


Thread

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

csiph-web