best way of testing a program exists before using it?

H

Hari Sekhon

I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be
better (although this will run on unix anyway, but it'd be nice if it
ran on windows too).


-h
 
R

Rob Wolfe

Hari said:
I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be
better (although this will run on unix anyway, but it'd be nice if it
ran on windows too).

IMHO this is pretty portable:
.... for p in os.environ['PATH'].split(os.pathsep):
.... if os.path.isfile(os.path.join(p, fname)):
.... return True
.... return False

HTH,
Rob
 
S

Steven Bethard

Hari said:
I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

There is the which module:

http://trentm.com/projects/which/

But I'd probably just try the command and catch the exception, e.g.:
.... subprocess.call(['foo'])
.... except OSError:
.... print "you don't have foo installed"
....
you don't have foo installed.... subprocess.call(['svm_learn'])
.... except OSError:
.... print "you don't have svm_learn installed"
....
1

STeVe
 
S

Steve Holden

Rob said:
Hari said:
I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be
better (although this will run on unix anyway, but it'd be nice if it
ran on windows too).


IMHO this is pretty portable:


.... for p in os.environ['PATH'].split(os.pathsep):
.... if os.path.isfile(os.path.join(p, fname)):
.... return True
.... return False
Except that the fname will then have to be a ".exe" on Windows and not
on Unix.

regards
Steve
 
R

Rob Wolfe

Steve Holden said:
Rob said:
Hari said:
I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be
better (although this will run on unix anyway, but it'd be nice if it
ran on windows too).
IMHO this is pretty portable:
def is_on_path(fname):
.... for p in os.environ['PATH'].split(os.pathsep):
.... if os.path.isfile(os.path.join(p, fname)):
.... return True
.... return False
Except that the fname will then have to be a ".exe" on Windows and not
on Unix.

My function doesn't check if the file is executable at all.
This function checks only if the file exists on the PATH.
I know it isn't a perfect solution but it is portable.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top