📜  将标准输出重定向到变量 python 代码示例

📅  最后修改于: 2022-03-11 14:46:46.836000             🧑  作者: Mango

代码示例1
from cStringIO import StringIO # Python3 use: from io import StringIO
import sys

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

# blah blah lots of code ...

sys.stdout = old_stdout

# examine mystdout.getvalue()