binary conversion issues

Grant Edwards grante at visi.com
Tue Aug 8 16:20:46 EDT 2006


On 2006-08-08, godavemon <davefowler at gmail.com> wrote:
> You guys are awesome!  I had to set the 'b' flag when writing the
> binaries.
>
> file_obj = open('filename.bin', 'wb')
>
> instead of just using 'w'
>
> It worked fine for all of the other 10 binary files I made,
> but had a small error with one of them.  In that one file's
> case it wrote out an extra 4 bytes in the middle somewhere.
> Strange.

If you leave off the 'b' (and you're running on windows), any
byte written to the output file that has a value of 0x0A will
get converted to the two byte sequence 0x0A 0x0D.  The extra
bytes in the resulting file are the 0x0D bytes that were
inserted after any 0x0A bytes in the written data.

If the data you write doesn't have any 0x0A bytes, then nothing
gets changed, and your file contains what you expect.  If the
data does does have 0x0A bytes, you get an extra 0x0D inserted
after each one.

(It wasn't _that_ wild-ass a guess, since a lot of people get
tripped up by the line-ending conversion issue.)

-- 
Grant Edwards                   grante             Yow!  Is it clean in other
                                  at               dimensions?
                               visi.com            



More information about the Python-list mailing list