how to count lines in a file ?

Bjorn Pettersen BPettersen at NAREX.com
Tue Jul 23 13:02:24 EDT 2002


> From: Denis S. Otkidach [mailto:ods at fep.ru] 
> 
> On Tue, 23 Jul 2002, Shagshag13 wrote:
> 
> S> hello,
> S>
> S> i need to count lines in a file (that i *can't* keep in memory, so 
> S> can't use readlines())
> 
> fp = open(filename)
> it = iter(fp)
> try:
>     while it.next():
>         count += 1
> except StopIteration:
>     pass

Now, why on earth would you use that monstrosity instead of:

  for line in open(filename):
      count += 1

???

-- bjorn





More information about the Python-list mailing list