对于 python3.4+,标准库中有一个上下文管理器。
with contextlib.redirect_stdout(file_like_object):
...
这部分答案已更新,但主要是针对那些仍然停留在 python2.x 世界的人
如果您坚持使用旧版本的 python,那么您自己编写这个上下文管理器并不难。关键是可以更新sys.stdout
到任何你想要的类似文件的对象(这就是print
写信给):
>>> 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
创建一个上下文管理器 http://docs.python.org/2/reference/datamodel.html#context-managers当您进入上下文时将标准输出设置为您想要的任何内容,然后让上下文管理器在您输入时重置标准输出__exit__
上下文。
这是一个简单的例子,使用contextlib
创建上下文管理器:
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:
在 python3.x 上,StringIO.StringIO
已移至io.StringIO
。另外,在 python2.x 上,cStringIO.StringIO
性能可能会稍微好一些。