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

Steven Bethard steven.bethard at gmail.com
Thu May 26 13:00:12 EDT 2005


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



More information about the Python-list mailing list