best way of testing a program exists before using it?
Hari Sekhon
hpsekhon at googlemail.com
Mon Sep 11 12:18:09 EDT 2006
Tim Williams wrote:
> 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 ...
>>
>> regards
>> Steve
>>
>>
>> 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.
>
> try:
> run_somecommand
> except:
> print "you don't have %s installed" % somecommand
>
>
> HTH :)
>
The down side to that is the program has to be run which consumes time
and slows the script down a fair bit (as well as outputting garbage to
the screen)
-h
--
Hari Sekhon
More information about the Python-list
mailing list