osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The file method read does not "see" CR


On Tue, Dec 10, 2019 at 9:17 PM Stephen Tucker <stephen_tucker at sil.org> wrote:
>
> I am running Python 2.7.10 on a Windows 10 machine. I have written a Python
> program that counts occurrences of bytes in a text file and have run it on
> a small test file. This file has CR-LF sequences at the ends of its lines.
> The Python program counted the LF bytes, but didn't count any CR bytes.
> Given that the documentation for the read method claims that exactly size
> bytes are read by each call to the method (in this case, size is 1) does
> this behaviour constitute a bug?
>

Are you trying to treat the file as text, or bytes? Are you looking
for occurrences of bytes, or characters? Make a decision on that, then
code accordingly. If you want to look for bytes, then open the file in
binary mode and read it as bytes. If you want to look for characters,
then you probably shouldn't care about which sort of line ending it
is, but if you really do, then disable newline processing and examine
the end of each logical line.

The read call will return one character, if the file is in text mode.

ChrisA