[Tutor] Detect errors when using os.popen.readlines()

Alan Gauld alan.gauld at btinternet.com
Sun Apr 1 01:01:49 CEST 2007


"Peter" <lists1 at marscode.net> wrote

> Is there a way to detect errors when running shell commands using
> os.popen?

You have to parse the programs output.
Usually errors will appear on stderr so you need to read that as
well as stdout.

This may be slightly easier using the new subprocess module
and the Popen class.

> if an interface doesn't exist I get an error from the shell command.
> I tried using try and except, but that did seem to work.

Even if the program returns an error popen is still working just
fine so no exception gets raised. You must parse the output
(or check the status value, but thats not reliable in all programs)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list