os independent way of seeing if an executable is on the path?

Discussion in 'Python' started by Steven Bethard, May 26, 2005.

  1. This has probably been answered before, but my Google skills have failed
    me so far...

    Is there an os independent way of checking to see if a particular
    executable is on the path? Basically what I want to do is run code like:
    i, o, e = os.popen3(executable_name)
    but I'd like to give an informative error if 'executable_name' doesn't
    refer to an executable on the path.

    The idea is to differentiate between errors generated by not being able
    to run the program, and errors generated while running the program. The
    former is a probably a configuration error by my user, the second is
    probably a logic error in my code (or perhaps an error on the executable
    I'm calling).

    In Windows, I can read the error file, and get something like:
    "'<program name>' is not recognized as an internal or external
    command,\noperable program or batch file.\n"
    and I'm sure I could parse this, but this seems fragile, and clearly os
    dependent.

    It's not crucial that I use os.popen3 as long as I have access to the
    input, output and error files. I played around with subprocess for a
    while, but couldn't see any way to do this using that module either.

    Thanks for the help,

    STeVe
    Steven Bethard, May 26, 2005
    #1
    1. Advertising

  2. Steven Bethard

    Don Guest

    Steven Bethard wrote:

    > This has probably been answered before, but my Google skills have failed
    > me so far...
    >
    > Is there an os independent way of checking to see if a particular
    > executable is on the path? Basically what I want to do is run code like:
    > i, o, e = os.popen3(executable_name)
    > but I'd like to give an informative error if 'executable_name' doesn't
    > refer to an executable on the path.
    >
    > The idea is to differentiate between errors generated by not being able
    > to run the program, and errors generated while running the program. The
    > former is a probably a configuration error by my user, the second is
    > probably a logic error in my code (or perhaps an error on the executable
    > I'm calling).
    >
    > In Windows, I can read the error file, and get something like:
    > "'<program name>' is not recognized as an internal or external
    > command,\noperable program or batch file.\n"
    > and I'm sure I could parse this, but this seems fragile, and clearly os
    > dependent.
    >
    > It's not crucial that I use os.popen3 as long as I have access to the
    > input, output and error files. I played around with subprocess for a
    > while, but couldn't see any way to do this using that module either.
    >
    > Thanks for the help,
    >
    > STeVe


    I wrote this 'which' function for Linux, but I think if you changed the ':'
    character, it would work on Windows (I think its a ';' on Windows, but I
    can't remember):

    def which( command ):
    path = os.getenv( 'PATH' ).split( ':' )
    found_path = ''
    for p in path:
    try:
    files = os.listdir( p )
    except:
    continue
    else:
    if command in files:
    found_path = p
    break

    return found_path


    -Don
    Don, May 26, 2005
    #2
    1. Advertising

  3. Steven Bethard

    Trent Mick Guest

    > > In Windows, I can read the error file, and get something like:
    > > "'<program name>' is not recognized as an internal or external
    > > command,\noperable program or batch file.\n"
    > > and I'm sure I could parse this, but this seems fragile, and clearly os
    > > dependent.


    http://starship.python.net/crew/tmick/#which

    I have a 'which' implementation that looks up a command-line program
    name in the same way that Windows' process launching does at the shell
    (when on Windows) and in the same way the usual 'which' executable does
    on Un*x (when on Un*x).

    Trent

    --
    Trent Mick
    Trent Mick, May 26, 2005
    #3
  4. Don wrote:

    > I wrote this 'which' function for Linux, but I think if you changed the ':'
    > character, it would work on Windows (I think its a ';' on Windows, but I
    > can't remember):

    Why remember when os.pathsep will do it for you.
    However, for windows you need a bit more:

    def which(command):
    win32 = sys.platform == 'win32'
    if win32:
    # Case-insesitive file names and file exts for commands
    try:
    knownexts = [''] + os.getenv('PATHEXT').split(os.pathsep)
    except AttributeError:
    knownexts = ['']
    else:
    knownexts = [ext.lower() for ext in knownexts]
    test = command.lower()
    tests = set(test + ext for ext in knownexts)
    else:
    tests = set([command])
    for dirname in os.getenv('PATH').split(os.pathsep):
    try:
    files = os.listdir(dirname)
    except IOError:
    continue
    else:
    for name in files:
    if name in tests or win32 and name.lower() in tests:
    yield dirname, name


    --Scott David Daniels
    Scott David Daniels, May 26, 2005
    #4
    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. MK
    Replies:
    1
    Views:
    822
    Peter Hansen
    Jun 25, 2003
  2. guesswho
    Replies:
    3
    Views:
    617
    John O
    Sep 6, 2006
  3. Replies:
    15
    Views:
    475
    Dave Thompson
    Aug 21, 2006
  4. pratap
    Replies:
    20
    Views:
    745
    Flash Gordon
    Mar 7, 2007
  5. PeterB
    Replies:
    1
    Views:
    123
    PeterB
    Feb 10, 2005
Loading...

Share This Page