how to count lines in a file ?

Michael Gilfix mgilfix at eecs.tufts.edu
Wed Jul 24 09:35:49 EDT 2002


  Er, hopefully this hasn't been suggested already, but perhaps
file.xreadlines() will do the trick? If all that needs to be done is
to count the file:

  for line in file.xreadlines():
      count += 1

                     -- Mike

On Wed, Jul 24 @ 12:20, James Kew wrote:
> *cough* Reread the OP's problem specification:
> 
> "Shagshag13" <shagshag13 at yahoo.fr> wrote in message
> news:ahk020$tl4ue$1 at ID-146704.news.dfncis.de...
> > i need to count lines in a file (that i *can't* keep in memory, so can't
> use readlines())
> 
> The "for line in file" solution addresses this constraint:
> "len(file.readlines())" doesn't.

-- 
Michael Gilfix
mgilfix at eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html




More information about the Python-list mailing list