best way of testing a program exists before using it?
Hari Sekhon
hpsekhon at googlemail.com
Mon Sep 11 11:55:04 EDT 2006
Rob Wolfe 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"?
>>
>> 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
>
> HTH,
> Rob
>
>
That works well and is portable, fits nicely. I was actually curious if
somebody already came across this and perhaps put a func like this in
the standard modules...
Thanks.
-h
--
Hari Sekhon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060911/ec3ce40c/attachment.html>
More information about the Python-list
mailing list