open() in binary vs. text mode

Syver Enstad syver-en+usenet at
Thu Mar 20 19:50:51 CET 2003

bobnotbob at (Bob Roberts) writes:

> I just finished tracking down a cross-platorm bug.  The problem was
> that I didn't open() a file in binary ("rb") mode.  What exactly does
> the binary flag do on windows?  What is it's purpose?

On windows text files are written with \r\n (carriage return and
linefeed) as the end of line marker. On Macintosh \r is the end of
line marker, and on *nix systems just \n is the end of line marker.

When opening a file in 'r' mode under windows the library translates
all the \r\n in your file into \n. When you write to a file with 'w'
under windows, library translates all \n your data to \r\n in the

To avoid this auto translation, you can specify b ('rb' or 'wb') to
indicate that the file library should not translate (useful for binary
files, therefore called binary or mode).

Hope this clears up things.


Vennlig hilsen 

Syver Enstad

More information about the Python-list mailing list