best way of testing a program exists before using it?
tim at tdw.net
Mon Sep 11 18:10:56 CEST 2006
On 11/09/06, Hari Sekhon <hpsekhon at googlemail.com> wrote:
> Steve Holden wrote:
> Hari Sekhon wrote:
> The easiest way to test whether the command will run is to try and run
> it. If the program doesn't exist then you'll get an exception, which you
> can catch. Otherwise you'll be stuck with non-portable mechanisms for
> each platform anyway ...
> Yeah, this occurred to me just after I sent the mail, but I don't really
> want to run the program because it will go off and do some work and take
> time to come back. If there is a better way then that would be great. I
> can't think of anything other than what you have suggested with a message
> saying that the program wasn't found in the path which would be the most
> appropriate error since the path could also be wrong.
If you run your wrapper and the program exists then you don't have to
test for it, so the overall process is quicker and cleaner than
testing-for *then* running the program
If you run your wrapper and the program doesn't exist, then you have
performed your "if exists" test without extra code and with very
little processing, and the raised exception will lead you nicely into
your "not exists" scenario.
print "you don't have %s installed" % somecommand
More information about the Python-list