best way of testing a program exists before using it?

Tim Chase python.list at tim.thechases.com
Mon Sep 11 12:18:49 EDT 2006


> Erm, but don't you *have* to run the program anyway to produce
> the required XML output? So, if the attempt to run it fails
> then you know it isn't installed, and if it succeeds then you
> should have the required output (I'm presuming either the
> output will appear in a file or you'll be using the subprocess
> module or similar to capture its standard output).

While it may not be the case for the OP, it's not entirely 
unreasonable there might be some intermediate process that is 
time-consuming...something like

def do_stuff():
	data = gather_data_from_user_for_x_days(7)
	do_something_with(data)
	os.system('program.exe') # to process the gathered data

If it's going to bomb out in step #3, it might be nice to know 
before you start the 7-days worth of data-gathering/sampling in 
step #1.

def do_stuff():
	if exists_in_path('program.exe'):
		data = gather_data_from_user_for_x_days(7)
		do_something_with(data)
		os.system('program.exe')
	else:
		let_user_know_cant_find_program()

That way, they can remedy the situtation before wasting their 
time with a laborious process.

Granted, there's a race condition, that if the program exists and 
the user starts gathering data, and then deletes the program 
before the completion of the data-gathering, it will bomb out. 
However, I'd chalk that up to a 
stupid-user-for-shooting-themselves-in-the-foot exception. :)

Sure, it's a bit contrived in this case, but I've had too many 
applications that do exactly this, and would have welcomed 
information that a piece was missing *before* I had invested time 
in the app.

-tkc








More information about the Python-list mailing list