printing bytes to stdout in Py3

Peter Billam peter at
Tue Feb 17 18:23:26 CET 2009

On 2009-02-17, Christian Heimes <lists at> wrote:
> Peter Billam schrieb:
>> 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 ?
> The official API to write binary data to stdout is:
>>>> count = sys.stdout.buffer.write(b"abc\n")
> abc

That's the one that works the best for me :-)
Thanks for all the suggestions!

Regards,  Peter

Peter Billam

More information about the Python-list mailing list