[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 09:07:08 -0400 (EDT)


On Thu, 6 Sep 2001, dman wrote:

> On Thu, Sep 06, 2001 at 02:50:04AM -0400, Ignacio Vazquez-Abrams wrote:
> | 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')
>
> Just beware of Mac's.  You won't find a single \n in a Mac text file
> because they use \r instead.  FYI in case you have to deal with a text
> file that came from a Mac.
>
> -D

Fair enough:

---
a=None
n=0
while not a='':
  a=file.read(262144)
  n+=a.count(os.linesep)
---

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