How to count lines in a text file ?

Andrew Dalke adalke at mindspring.com
Wed Sep 22 15:53:33 EDT 2004


Alex Martelli wrote:
> If one is really in a hurry counting lines, a dedicated C extension
> might help.  E.g.:
> 
> static PyObject *count(PyObject *self, PyObject *args)
    ...
> Using this count-items-in-iterable thingy

There's been a few times I've wanted a function like
this.  I keep expecting that len(iterable) will work,
but of course it doesn't.

Would itertools.len(iterable) be useful?  More likely
the name collision with len itself would be a problem,
so perhaps itertools.length(iterable).


BTW, I saw itertools.count and figured that might be
it.  Nope.  And don't try the following

 >>> import itertools
 >>> itertools.count(5)
count(5)
 >>> print list(_)

   :)

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list