[Tutor] how to redirect sys.__stdout__ back to screen in IDLE

alan.gauld@bt.com alan.gauld@bt.com
Sun Nov 17 12:05:03 2002


> I was fiddling IDLE (Python2.2, OS:Win32) with

>>> sys.stdout = open('test.txt','w')
>>> print 'foobar'
>>> sys.stdout.close()
>>> sys.stdout = sys.__stdout__
>>> print 'foobar'
foobar
>>>

Works for me OK.

Interestingly I got all sorts of strange error messages when I tried 
to check sys.__stdout__ before reassigning it to sys.__stdout__ but 
after assignment it was OK...

>>> sys.__stdout__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: I/O operation on closed file
>>> sys.stdout = sys.__stdout__
>>> print 'foo'
foo
>>> sys.__stdout__
<open file '<stdout>', mode 'w' at 0x973648>
>>>

I don't know why that was... if anyone has a reason I'm interested.

Alan G