best way of testing a program exists before using it?

Steven Bethard steven.bethard at gmail.com
Mon Sep 11 11:51:04 EDT 2006


Hari Sekhon wrote:
> 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.:

 >>> import subprocess
 >>> try:
...     subprocess.call(['foo'])
... except OSError:
... 	print "you don't have foo installed"
...
you don't have foo installed
 >>> try:
...     subprocess.call(['svm_learn'])
... except OSError:
... 	print "you don't have svm_learn installed"
...
1

STeVe



More information about the Python-list mailing list