file read, binary or text mode

Roel Schroeven rschroev_nospam_ml at
Fri Sep 24 16:38:38 CEST 2004

Guyon Morée wrote:

> ok, i have huffman encoding code.
> this is actually build for text, but because python can also read a binary
> file as a string, this applies equally well :)
> but, i was just wondering if this gives any problems if I use text-mode read
> for the binary files and vice versa.
> If I undertand correctly now, using binary mode is _always_ save, right?

It's safe in the sense that everything goes out exactly as it came in. 
For example, gzip uses binary mode even when compressing text files. The 
files may be text, but gzip doesn't care about that. It doesn't care 
about words, sentences and line endings, but it does care about 
representing exactly the bytes that are in the file.

Editors, diff, wc, ... use text mode.
cp, tar, gzip, ... use binary mode.

"Codito ergo sum"
Roel Schroeven

More information about the Python-list mailing list