Writing a stream of bytes

Tim Chase python.list at tim.thechases.com
Fri Jan 15 11:08:30 EST 2016


[sorry, toddler on my lap clicked <send> before I could type]

> import struct
> with open('toto', 'wb') as f: f.write(struct.pack('<4B', *[1,2,3,4]))

This one does what you want.  The problem resides in your check:

> I always end up with the following bytes on file:
> !hexdump toto
> 0000000 0201 0403

Hexdump is grouping them.  Try the canonical format:

 !hexdump -C toto
 00000000  01 02 03 04              |....|
 00000004


-tkc






More information about the Python-list mailing list