[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 16:22:22 +0200


On  0, dman <dsh8290@rit.edu> 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.

If the file is opened in text mode, then that is Python's problem (actually
the C library's), not yours. From the Python side it all looks like \n,
whatever the system.

-- 
Remco Gerlich