best way of testing a program exists before using it?

Hari Sekhon hpsekhon at googlemail.com
Mon Sep 11 12:03:41 EDT 2006


Steven Bethard wrote:
> 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
>   
looks good, although I agree with you on the try it and see simplicity. 
I think if the which.py was part of the standard lib then I would use 
it, but as it is I will stick to the original path search that you 
recommended (I'm not gonna both dealing with the pathext just yet)

-- 
Hari Sekhon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060911/efd4df41/attachment.html>


More information about the Python-list mailing list