Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   os independent way of seeing if an executable is on the path? (http://www.velocityreviews.com/forums/t345370-os-independent-way-of-seeing-if-an-executable-is-on-the-path.html)

Steven Bethard 05-26-2005 05:00 PM

os independent way of seeing if an executable is on the path?
 
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

Don 05-26-2005 06:53 PM

Re: os independent way of seeing if an executable is on the path?
 
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



Trent Mick 05-26-2005 07:37 PM

Re: os independent way of seeing if an executable is on the path?
 
> > 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
TrentM@ActiveState.com

Scott David Daniels 05-26-2005 07:46 PM

Re: os independent way of seeing if an executable is on the path?
 
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.Daniels@Acm.Org


All times are GMT. The time now is 07:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.