file read, binary or text mode

Grant Edwards grante at
Fri Sep 24 14:26:53 CEST 2004

On 2004-09-24, Guyon Morée <gumuz at> wrote:

> ok, i have huffman encoding code.

You should open the file in binary.

> this is actually build for text,

All of the Huffman encoding implimentations I've seen output
binary, but I'll take your word for it.

> but because python can also
> read a binary file as a string, this applies equally well :)

If the file contains printiable text with cr/nl, nl, or cr line
endings, then open it in text mode.  Otherwise open it in
binary mode.

> but, i was just wondering if this gives any problems if I use
> text-mode read for the binary files and vice versa.

Yes, it will give you problems.

> If I undertand correctly now, using binary mode is _always_ save, right?


If it's text, open it in text mode.  That way the line endings
are handled properly.

Grant Edwards                   grante             Yow!  I think I'll do BOTH
                                  at               if I can get RESIDUALS!!

More information about the Python-list mailing list