Redirect stdout
Steve Purcell
stephen_purcell at yahoo.com
Wed Apr 11 10:13:36 EDT 2001
Fernando Rodríguez wrote:
> How can I temporarely redirect stdout to a string?
>
Use the StringIO module:
>>> import sys, StringIO
>>> print 1
1
>>> stdold, stdnew = sys.stdout, StringIO.StringIO()
>>> sys.stdout = stdnew
>>> print 2
>>> sys.stdout = stdold
>>> print 3
3
>>> stdnew.getvalue()
'2\012'
>>>
-Steve
--
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo
More information about the Python-list
mailing list