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


Groups > comp.lang.python > #29583

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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.001
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; 'probably': 0.29; '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; 'skip:o 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'necessarily': 0.63; 'replicate': 0.84; 'write:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Gelonida N <gelonida@gmail.com>
Subject Re: portable way of locating an executable (like which)
Date Fri, 21 Sep 2012 01:15:02 +0200
References <k3g0h6$mtr$1@ger.gmane.org> <CAEk9e3rfcvbrjVpTUmX9RdtmpExe_owxfrEwJqDREzfUmNGR0w@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host unicorn.dungeon.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0
In-Reply-To <CAEk9e3rfcvbrjVpTUmX9RdtmpExe_owxfrEwJqDREzfUmNGR0w@mail.gmail.com>
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.981.1348182923.27098.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348182923 news.xs4all.nl 6858 [2001:888:2000:d::a6]:32951
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29583

Show key headers only | View raw


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

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


Thread

Re: portable way of locating an executable (like which) Gelonida N <gelonida@gmail.com> - 2012-09-21 01:15 +0200

csiph-web