error writing str to binary stream - fails in Python 3.0.1, works in 2.x

Scott David Daniels Scott.Daniels at Acm.Org
Mon Mar 16 23:44:32 CET 2009


R. David Murray wrote:
>...
> Here is some example code that works:
> 
>     out=open('temp', "wb")
>     out.write(b"BM")
> 
>     def write_int(out, n):
>         bytesout=bytes(([n&255), (n>>8)&255, (n>>16)&255, (n>>24)&255])
>         out.write(bytesout)
>     write_int(out, 125)

or even:
     import struct
     ...
     def write_int(out, n):
         out.write(struct.pack('<l', n))
     write_int(out, 125)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list