How to capture stdout in an exec'ed string?

Preston Landers preston at
Wed Nov 17 22:22:08 EST 1999

Thanks a lot Mark!  It only took a few microseconds after I posted to
realize what I needed to do.  For some reason the concept of saving
and restoring the stdout handler didn't occur to me.  I was thinking
about namespace issues which are not really related.


"Mark Hammond" <mhammond at> writes:

> Something like this should work:
> try:
>    oldout = sys.stdout
>    sys.stdout = My_STDOUT()
>    try:
>      cruft = my_ancient_class()
>      cruft.do_your_thang()
>    finally:
>      sys.stdout = oldout
> except SystemExit, msg:
>    process(msg)
> Mark.

More information about the Python-list mailing list