how to count lines in a file ?

Alex Martelli aleax at aleax.it
Wed Jul 24 11:50:16 EDT 2002


Shagshag13 wrote:
        ...
> this :
> for line in open(filename):
>     count += 1
> 
> or this :
> fhi = file(filename)
> while 1:
>      lines = fhi.readlines(bufferSize)
>      if not lines:
>         break
>      lineCount += len(lines)

Two clear and obvious lines or six complicated and mysterious
ones?  Hmmm, a hard question indeed.

BTW, GvR claimed (less than a month ago on the python-dev list)
that having an integer argument to readlines was a design error.

So, two clear and obvious lines using the language release that
has been current for the last 7 months, or six complicated and
mysterious ones using what the language designer now considers
a design error?  The choice is becoming harder and harder...


Alex




More information about the Python-list mailing list