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

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

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

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. Eli Bendersky
    Replies:
    1
    Views:
    1,496
    Mike Treseler
    Mar 1, 2006
  2. =?Utf-8?B?c3VyZXNoX0Mj?=
    Replies:
    8
    Views:
    8,525
    =?Utf-8?B?TWFoZXNoIE5pbWJhbGthcg==?=
    May 3, 2005
  3. Replies:
    7
    Views:
    1,181
  4. octangle
    Replies:
    2
    Views:
    321
    Skarmander
    Jun 16, 2006
  5. Replies:
    15
    Views:
    666
    Dave Thompson
    Aug 21, 2006
  6. pratap
    Replies:
    20
    Views:
    1,042
    Flash Gordon
    Mar 7, 2007
  7. Jason Cavett
    Replies:
    8
    Views:
    947
    Sanjay
    May 16, 2007
  8. Replies:
    0
    Views:
    603
Loading...