Terry J. Reedy added the comment:
If you do the same in the standard python REPL, you get the same result. So
this behavior is not specific to IDLE, although one has to be slightly more
careful restoring sys.stdout in IDLE. (Restoring stdout from saved stdout
should work the same without
Raymond Hettinger added the comment:
If your goal is to temporarily redirect stdout, there are a couple ways to do
it:
with open('dest1.txt', 'w') as target_file:
print('hello world', file=target_file)
or:
with open('dest1.txt', 'w') as target_file:
with
New submission from Alex <2423067...@qq.com>:
Hi.
This bug I've found is about the redirect, you know, to redirect the standard
input/output stream. It's not really a bug, you can think of it as a feature.
The code is like this:
import sys
sys.stdout = open('redirect.txt','w')
print('hello