[issue38740] Line count mis match between open() vs sys.stdin api calls

Steven D'Aprano report at bugs.python.org
Fri Nov 8 04:04:49 EST 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

This seems to be the difference between Universal Newlines or not. In Python 2, you have to set it explicitly with a U in the open mode:

    $ python2.7 -c 'import sys; print("Linecount=", sum(1 for x in open(sys.argv[1], "Ur")))' line_break_err.txt
    ('Linecount=', 18)

In Python 3, Universal Newlines is the default for text files, but you can control it with the ``newline`` parameter:

    $ python3.5 -c 'import sys; print("Linecount=", sum(1 for x in open(sys.argv[1], newline="\n")))' line_break_err.txt
    Linecount= 9


    $ python3.5 -c 'import sys; print("Linecount=", sum(1 for x in open(sys.argv[1], newline="\r")))' line_break_err.txt
    Linecount= 15


I think this explains the difference you are seeing. Do you agree?

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38740>
_______________________________________


More information about the Python-bugs-list mailing list