On 15/01/2016 15:55, jmp wrote:
Hi pyple !
I'd like to write a stream of bytes into a file. I'd like to use the
struct (instead of bytearray) module because I will have to write more
than bytes.
let's say I want a file with 4 bytes in that order:
01 02 03 04
None of these work:
import struct
with open('toto', 'wb') as f: f.write(struct.pack('4B', *[1,2,3,4]))
with open('toto', 'wb') as f: f.write(struct.pack('<4B', *[1,2,3,4]))
with open('toto', 'wb') as f: f.write(struct.pack('>4B', *[1,2,3,4]))
I always end up with the following bytes on file:
!hexdump toto
0000000 0201 0403
Why is the hex dump doing 16-bits at a time? Get a byte dump, then it
might actually be the right output.
Anyway, this seems to work:
data=bytearray([1,2,3,4])
f = open("output", "wb")
f.write(data)
f.close()
Doubtless there are plenty of other ways.
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list