how to count lines in a file ?

James Kew james.kew at btinternet.com
Wed Jul 24 07:20:25 EDT 2002


"Bo M. Maryniuck" <b.maryniuk at forbis.lt> wrote in message
news:mailman.1027501596.4354.python-list at python.org...
> On Tuesday 23 July 2002 19:02, Bjorn Pettersen wrote:
> > Now, why on earth would you use that monstrosity instead of:
> >
> >  for line in open(filename):
> >      count += 1
>
> Or even:
>
> print len(open('/etc/passwd').readlines())

*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.

James






More information about the Python-list mailing list