Capture output from stderr

Grant Edwards grante at
Wed Aug 6 05:47:10 CEST 2003

In article <mailman.1060136229.29900.python-list at>, Jeff Epler wrote:

>> I've always used a read handler for this in the past, and it worked fine.
>> I don't have an example handy...

I was about to look up my example program, but you beat me to it. ;)

> Here's a little program I just put together, it shows the output from a
> command in a scrolling text area, using popen and
> _tkinter.createfilehandler.


> import Tkinter, _tkinter, fcntl, os, sys
> p = os.popen(command, "r")
> pf = p.fileno()
> # Make reads from the popen'd command nonblocking
> # (so read returns the bytes available without waiting)
> fcntl.fcntl(pf, fcntl.F_SETFL, os.O_NONBLOCK)
> def readfunc(fileobj, event_type):
> 	bytes =

I always use rather than the file object's read method to gaurd
against cases where the file descriptor is readable when the file object
isn't. I don't know if that ever really happens, but it seemed to be the
safe thing to do.  I suspect that your setting the file descriptor to
non-blocking accomplishes the same thing.

> 	if bytes == '':
> 		bytes = "***END OF OUTPUT***"
> 		t.wm_title("%s - COMPLETED - tktail" % command)
> 		_tkinter.deletefilehandler(p)

> _tkinter.createfilehandler(p, Tkinter.READABLE, readfunc)
> t.mainloop()

Grant Edwards                   grante             Yow!  I need "RONDO".

More information about the Python-list mailing list