How can I turn stdout to the terminal back, when use 'sys.stdout' on Google Colab Notebook in Python.
Recommend that first the original output is stored to a variable.
import sys print(sys.__stdout__) print(sys.stdout) # <_io.TextIOWrapper name='Those are differen. Once output to a file, the system does not turn to the original output system easily.' mode='w' encoding='UTF-8'> # <ipykernel.iostream.OutStream object at 0x7f1042f742d0>
Recommend that first the original output is stored to a variable.
temp_stdout = sys.stdoutFinally you can change the stdout to a file.
sys.stdout = open('out.txt', 'w') print('Output to the file.') sys.stdout = sys.__stdout__ print('Where is output?') sys.stdout = temp_stdout print('Output to the terminal')
Comments
Post a Comment