Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
user1561108 Avatar asked Oct 15 '25 17:10

user1561108


1 Answers

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.

like image 100
tynn Avatar answered Oct 18 '25 07:10

tynn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!