portable way of locating an executable (like which)

Discussion in 'Python' started by Gelonida N, Sep 20, 2012.

  1. Gelonida N

    Gelonida N Guest

    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?

    Thanks for any sugestions
     
    Gelonida N, Sep 20, 2012
    #1
    1. Advertising

  2. Gelonida N

    Nobody Guest

    On Thu, 20 Sep 2012 23:06:46 +0200, 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.


    Note that "which" attempts to emulate the behaviour of execvp() etc. The
    exec(3) manpage will explain the precise algorithm used (e.g. they skip
    files for which the process lacks execute permission).

    Also, note that the shell has built-in commands, functions, and aliases in
    addition to programs. The "type" built-in command performs a similar
    function to "which" but using the shell's semantics. On some systems,
    the default configuration may alias "which" to "type".

    On Windows, there's a host of different "execute program" interface, all
    with subtly different semantics: which extensions they will run, which
    extensions can be omitted, which paths are used (e.g. %PATH%, paths
    from the registry, current directory).
     
    Nobody, Sep 21, 2012
    #2
    1. Advertising

  3. Gelonida N

    Tarek Ziadé Guest

    On 9/21/12 1:59 AM, Nobody wrote:
    > On Thu, 20 Sep 2012 23:06:46 +0200, 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.

    > Note that "which" attempts to emulate the behaviour of execvp() etc. The
    > exec(3) manpage will explain the precise algorithm used (e.g. they skip
    > files for which the process lacks execute permission).
    >
    > Also, note that the shell has built-in commands, functions, and aliases in
    > addition to programs. The "type" built-in command performs a similar
    > function to "which" but using the shell's semantics. On some systems,
    > the default configuration may alias "which" to "type".
    >
    > On Windows, there's a host of different "execute program" interface, all
    > with subtly different semantics: which extensions they will run, which
    > extensions can be omitted, which paths are used (e.g. %PATH%, paths
    > from the registry, current directory).
    >

    You can also look at shutil.which

    http://hg.python.org/cpython/file/aa153b827d17/Lib/shutil.py#l974


    Mmmm I wonder why it's removed in the last revs..
     
    Tarek Ziadé, Sep 21, 2012
    #3
  4. On Friday, 21 September 2012 02:37:01 UTC+5:30, gelonida 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?
    >
    >
    >
    > Thanks for any sugestions


    shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
    You can copy the code to support older Python versions.
     
    Ramchandra Apte, Sep 23, 2012
    #4
  5. On Friday, 21 September 2012 02:37:01 UTC+5:30, gelonida 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?
    >
    >
    >
    > Thanks for any sugestions


    shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
    You can copy the code to support older Python versions.
     
    Ramchandra Apte, Sep 23, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Lawrence
    Replies:
    0
    Views:
    180
    Mark Lawrence
    Sep 20, 2012
  2. Chris Angelico
    Replies:
    0
    Views:
    207
    Chris Angelico
    Sep 20, 2012
  3. Ian Kelly
    Replies:
    0
    Views:
    218
    Ian Kelly
    Sep 20, 2012
  4. Chris Angelico
    Replies:
    0
    Views:
    233
    Chris Angelico
    Sep 21, 2012
  5. Gelonida N
    Replies:
    0
    Views:
    196
    Gelonida N
    Sep 21, 2012
Loading...

Share This Page