A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/080896.html below:

Redirect stdout

Redirect stdout Redirect stdoutSteve 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4