[Tutor] What is the best way to count the number of lines in a huge file?
Remco Gerlich
scarblac@pino.selwerd.nl
Thu, 6 Sep 2001 10:25:17 +0200
On 0, HY <pythonpython@hotmail.com> 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.
That's not huge :)
On new PCs I'd just read it all in and count the \n's, but I'll assume here
that we can't do that.
> 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?
Well, this one works and is quite simple. Look no further unless its
performance is unacceptable.
I like Ignacio's solution best: read the file in chunks of acceptable size,
and count the '\n' characters. Don't see how it can be more efficient than
that.
--
Remco Gerlich