[ACCEPTED]-How can I redirect print output of a function in python-stdout

Accepted answer
Score: 20

For python3.4+, there's a context manager 13 for this in the standard library.

with contextlib.redirect_stdout(file_like_object):
    ...

This part of the answer was updated, but is mostly for people who are still stuck in a python2.x world

If you're 12 stuck on an older version of python, this 11 context manager isn't too hard to write 10 yourself. The key is that you can update 9 sys.stdout to whatever file-like object you want (that's 8 what print writes to):

>>> import sys
>>> import StringIO
>>> stdout = sys.stdout  # keep a handle on the real standard output
>>> sys.stdout = StringIO.StringIO() # Choose a file-like object to write to
>>> foo() 
>>> sys.stdout = stdout
>>> foo()
bar

To create a context manager to set stdout 7 to whatever you want when you enter the 6 context and then have the context manager 5 reset stdout when you __exit__ the context.

Here's 4 a simple example using contextlib to create the context 3 manager:

import contextlib
import sys

@contextlib.contextmanager
def stdout_redirect(where):
    sys.stdout = where
    try:
        yield where
    finally:
        sys.stdout = sys.__stdout__

def foo():
    print 'bar'

# Examples with StringIO
import StringIO

with stdout_redirect(StringIO.StringIO()) as new_stdout:
    foo()

new_stdout.seek(0)
print "data from new_stdout:",new_stdout.read()

new_stdout1 = StringIO.StringIO()
with stdout_redirect(new_stdout1):
    foo()

new_stdout1.seek(0)
print "data from new_stdout1:",new_stdout1.read()

# Now with a file object:
with open('new_stdout') as f:
    with stdout_redirect(f):
        foo()

# Just to prove that we actually did put stdout back as we were supposed to
print "Now calling foo without context"
foo()

Note:

On python3.x, StringIO.StringIO has moved to 2 io.StringIO. Also, on python2.x, cStringIO.StringIO might be slightly 1 more performant.

Score: 7

In Python 3.x, you can just redefine print.

B = []

def print(str):
    global B
    B.append(str)

def A():
    print("example")

A()

>>> B
['example']

If, for 2 some reason, you need the built-in print 1 back, just do:

from builtins import print

More Related questions