printing bytes to stdout in Py3

Gabriel Genellina gagsl-py2 at
Tue Feb 17 05:17:37 EST 2009

En Tue, 17 Feb 2009 03:04:23 -0200, Peter Billam <peter at>  

> Greetings. (Newbie warning as usual) In Python3, sys.stdout is a
> io.TextIOWrapper object; but I want to output bytes
>   (e.g. muscript -midi t > t.mid )
> and they're coming out stringified :-(  How can I either change the
> encoding on sys.stdout, or close sys.stdout and reopen it in 'b'
> mode, or dup(fd) it first, or whatever the right thing to do is ?

I cannot test this with Python 3 right now, but I think this should work  
(right at the start of your script):

sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb')

Gabriel Genellina

More information about the Python-list mailing list