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


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

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

Started byDave Angel <d@davea.name>
First post2012-09-20 22:31 -0400
Last post2012-09-21 12:13 +0200
Articles 2 — 2 participants

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) Dave Angel <d@davea.name> - 2012-09-20 22:31 -0400
    Re: portable way of locating an executable (like which) Hans Mulder <hansmu@xs4all.nl> - 2012-09-21 12:13 +0200

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

FromDave Angel <d@davea.name>
Date2012-09-20 22:31 -0400
SubjectRe: portable way of locating an executable (like which)
Message-ID<mailman.985.1348194714.27098.python-list@python.org>
On 09/20/2012 06:04 PM, Jason Swails wrote:
> On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N <gelonida@gmail.com> 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?
>>
> 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 don't have a Windows machine set up right now, but I believe there are
two more directories to search, besides the ones described in the PATH
variable.

One is the current directory, and the other is the Windows directory
(maybe also the xxx/system32 or something).

They don't have analogues in Linux or Mac, as far as I know.

-- 

DaveA

[toc] | [next] | [standalone]


#29618

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-21 12:13 +0200
Message-ID<505c3ddd$0$6883$e4fe514c@news2.news.xs4all.nl>
In reply to#29589
On 21/09/12 04:31:17, Dave Angel wrote:
> On 09/20/2012 06:04 PM, Jason Swails wrote:
>> On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N <gelonida@gmail.com> 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?
>>>
>> 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):

On Posix systems, you need to insert at this point:

            if not path:
                path = "."

>>          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 don't have a Windows machine set up right now, but I believe there are
> two more directories to search, besides the ones described in the PATH
> variable.
> 
> One is the current directory, and the other is the Windows directory
> (maybe also the xxx/system32 or something).
> 
> They don't have analogues in Linux or Mac, as far as I know.

On Posix system (inlcuding Linux and Mac OS X), the current
directory is not searched by default.  If there's an empty
string in os.getenv("PATH").split(os.pathsep), then the
current directory will be searched at that point in the part.


Hope this helps,

-- HansM


[toc] | [prev] | [standalone]


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


csiph-web