Python - 将标准输出重置为正常,之前将其重定向到文件
问题描述:
在我的 python 程序的开头,我有以下一行:
At the beginning of my python program I have the following line:
sys.stdout = open('stdout_file', 'w')
在我的程序进行到一半时,我想将 stdout 设置回正常的 stdout.我该怎么做?
Halfway through my program I would like to set stdout back to the normal stdout. How do I do this?
答
原始 stdout
可以作为 sys.__stdout__
访问.此已记录.
The original stdout
can be accessed as sys.__stdout__
. This is documented.