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

S

Steven Bethard

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
 
D

Don

Steven said:
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
 
T

Trent Mick

In Windows, I can read the error file, and get something like:
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
 
S

Scott David Daniels

Don said:
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top