sys.stdout not reassigning to sys.__stdout__

Posted by Vince on Stack Overflow See other posts from Stack Overflow or by Vince
Published on 2012-10-04T01:55:45Z Indexed on 2012/10/04 3:37 UTC
Read the original article Hit count: 427

Filed under:
|

I'm pretty new to Python so I am still just learning the language. One of the things I came across was reassigning sys.stdout to change the default output of print. So I wrote this as a test:

import sys
sys.stdout = open('log.txt','a')
print('hey')
sys.stdout.close()
sys.stdout = sys.__stdout__
print('hi')

Now the string 'hi' is not written to the file but it does not show in the default output either. When I assign some other variable to sys.stdout in the beginning and change it back it works, but I'm just wondering why its not changing back the first time.

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x