Cursed newlines and readline()

Steve Holden sholden at
Fri Sep 13 15:00:56 EDT 2002

"Thomas Wouters" <thomas at> wrote ...
> On Thu, Sep 12, 2002 at 06:59:14PM +0000, Edward K. Ream wrote:
> > According to one of Leo's Linux users, the Python's readline() routine
> > Linux delivers "\r\n" line end strings verbatim, while the windows
> > force the string to use the Unix convention of using only "\n". This
> > unfortunate.  Is there a justification for it?  If not, I would like to
> > suggest that readline() work the same on all platforms and that it
> > only "\n" as a line terminator.
> You can open the file in 'binary' mode (adding 'b' to the mode string) and
> the file will contain '\r\n' on both platforms (and any other platforms.)
Nope. Exactly wrong. In 2.2 and those before, when files are opened in
*text* mode (no "b") then reading them will provide Unix-style line endings
(newline only). When you open files in binary mode then you see the bytes
stored in the file.

On Unix systems there's no difference in the contents of a file whether in
binary or text mode. On Windows a file is shorter by the number of carriage
returns. On the Mac I have no idea what they do. Probably just carriage
returns, to be different :-)

2.3 will be a bit more flexible about such mattrers.

Steve Holden                        
Python Web Programming       
Previous .sig file retired to          

More information about the Python-list mailing list