[Tutor] What is the best way to count the number of lines in a huge file?

Ignacio Vazquez-Abrams ignacio@openservices.net
Thu, 6 Sep 2001 02:50:04 -0400 (EDT)


On Thu, 6 Sep 2001, HY wrote:

> What is the best way to count the number of lines in a huge file?
> Say, I have a file which is 15MB in size.
> I used:
>
> file=open("data.txt","r")
> n=0
> for line in file.xreadlines():
>     n+=0
> print n
>
> Is there a better/simpler way to do it?
>
> Thanks a lot.
>
> Hy

a=None
n=0
while not a=='':
  a=file.read(262144) # season to taste
  n+=a.count('\n')

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>