pythoniostdoutstringioredirectstandardoutput

How to reattach sys.stdout to console window in python?


My python 3 doodling went like this:

import io, sys
sys.stdout = io.StringIO()
# no more responses to python terminal funnily enough

My question is how to reattach so when I pass in 1+1 for example it'll return with 2 to the console?

This is in the python interpreter on 32-bit python running on windows 7 64 bit.


Solution

  • You're looking for sys.__stdout__:

    It can also be used to restore the actual files to known working file objects in case they have been overwritten with a broken object. However, the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object.