catching error output

Donn Cave donn at u.washington.edu
Tue Feb 12 15:45:25 EST 2002


Quoth gte743n at cad.gatech.edu (Joseph Holland King):
| in a small script i am using the popen command to run a command 
| and then parse the output:
| fs = os.popen('foo some_data')
| fs.readline()
| ...
| however when foo fails i need to catch that error and be able to parse
| it. is there anyway to do this? fs = .. does not catch it just the 
| normal output. thanks.

That sounds about like popen2.popen3().  You can close the input pipe
it also returns.

Might be safer to use the Python class that popen3() creates, though.

>>> import popen2
>>> c = popen2.Popen3('echo hi; echo ouch >&2; exit 1', 1)
>>> c.poll()
256                            # this is exit status 1 * 256
>>> c.childerr.readline()      # OK because process has exited
'ouch\012'

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list