Does Python mess with CRLFs?

Irmen de Jong irmen.NOSPAM at xs4all.nl
Wed Nov 12 19:42:15 CET 2008


Gilles Ganault wrote:
> On Wed, 12 Nov 2008 12:04:07 +0100, Gilles Ganault <nospam at nospam.com>
> wrote:
>> I wonder if Python rewrites CRLFs when reading a text file with
>> open/read?
> 
> For those seeing the same thing, the answer is yes: On Windows, the
> code above turns CRLF into LF. I tried "rb" instead of "r", with no
> difference.

Sorry but that is not what's happening. Your problem is not in reading the
file, it's in the regular expression you're using.

Using open with the "rb" flag leaves the file content intact and does not munge newlines 
in any way. A read() will return the exact bytes that are in the file.

--irmen



More information about the Python-list mailing list