[Python-ideas] Replacing the standard IO streams (was Re: changing sys.stdout encoding)

Serhiy Storchaka storchaka at gmail.com
Sat Jun 9 22:02:19 CEST 2012

On 09.06.12 12:55, Nick Coghlan wrote:
> So, after much digging, it appears the *right* way to replace a
> standard stream in Python 3 after application start is to do the
> following:
>      sys.stdin = open(sys.stdin.fileno(), 'r',<new settings>)
>      sys.stdout = open(sys.stdout.fileno(), 'w',<new settings>)
>      sys.stderr = open(sys.stderr.fileno(), 'w',<new settings>)

     sys.stdin = io.TextIOWrapper(sys.stdin.detach(), <new settings>)
     sys.stdout = io.TextIOWrapper(sys.stdout.detach(), <new settings>)

None of these methods are not guaranteed to work if the input or output 
have occurred before.

More information about the Python-ideas mailing list