how to count lines in a file ?

Bengt Richter bokr at oz.net
Thu Jul 25 17:52:08 EDT 2002


On Thu, 25 Jul 2002 08:52:53 GMT, Alex Martelli <aleax at aleax.it> wrote:
[...]
>
>f.readline(N) may return a partial, incomplete line if
>that's the best it can do without exceedinng N bytes.
>
>Docs for f.readlines(N) are not quite as clear on this
>point, but it seems that readlines is NOT allowed to
>return incomplete lines -- rather it should return more
>than N bytes (as it's explicitly allowed to) if needed.
>
That bothers me. ISTM it should return as many lines as
will fit in N bytes, but no more. Otherwise you have no
memory use protection against a single monster line, and
isn't capping memory use what N is about? Otherwise N
should be lines, not bytes.

I don't think it should be an insurmountable problem to
do the requisite buffering of starts of last lines not
fitting in N and feed them to the next readlines().

>That's definitely what the current implementation does.
>
Boo ;-)

>Module xreadlines (and thus f.xreadlines, which is
>explicitly documented as being equivalent to a call to
>xreadlines.xreadlines(f)) uses f.readlines(N) (both by
>the docs, and by the implementation).
>
Booboo, ISTM ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list