Python - reset stdout to normal, after previously

2019-02-08 05:25发布

问题:

At the beginning of my python program I have the following line:

sys.stdout = open('stdout_file', 'w')

Halfway through my program I would like to set stdout back to the normal stdout. How do I do this?

回答1:

The original stdout can be accessed as sys.__stdout__. This is documented.



回答2:

The same holds for stderr, of course. At the end, these lines are needed to get the original streams.

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__


标签: python stdout