readling newlines

Peter Otten __peter__ at web.de
Sun Jan 11 03:57:32 EST 2004


Alessandro Crugnola *sephiroth* wrote:

> Hi, I'm trying to detect the newlines method of a file opened.
> I'm using the 'U' mode as parameter when opening a file, but for every
> file opened the result is always "\r\n", even if the file has been saved
> with the "\n" newline method. Is there i'm missing?
> 
> I'm on Windows with python 2.3.3

Here's what I get (on Linux):

>>> for nl in ["\n", "\r\n", "\n\r", "\r"]:
...     dst = file("tmp.txt", "wb")
...     dst.write("one%stwo" % nl)
...     dst.close()
...     print repr(file("tmp.txt", "U").read())
...
'one\ntwo'
'one\ntwo'
'one\n\ntwo'
'one\ntwo'
>>>

>From the documentation of file():

If Python is built without universal newline support mode 'U' is the same as
normal text mode. 

So maybe you have a version without universal newline support?

Peter




More information about the Python-list mailing list