"readline()" omitted '\r' in win32 for dos format text file

N

Newgene

Hi, group,
I have python2.3 installed on win2k. I noticed that when I open a
dos format text file (eol is '\r\n'), readline() always returns a line
ending with '\n' only, not '\r\n'. While I read the same file on unix,
it returns a line ending with '\r\n' correctly.
This makes me difficult to determine the format of a text file, dos or
unix.

Is this a bug or intended behavior? If not a bug, then how to
determine the format of a text file?

Thanks.

C
 
P

Peter Otten

Newgene said:
I have python2.3 installed on win2k. I noticed that when I open a
dos format text file (eol is '\r\n'), readline() always returns a line
ending with '\n' only, not '\r\n'. While I read the same file on unix,
it returns a line ending with '\r\n' correctly.
This makes me difficult to determine the format of a text file, dos or
unix.

Is this a bug or intended behavior? If not a bug, then how to
determine the format of a text file?

It's not a bug. On windows, files are opened in text mode by default.

Opening a file in binary mode

file(name, "rb")

will preserve the line endings regardless of platform while opening it in
"universal" mode

file(name, "rU")

will normalize all line endings to "\n".

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top