how to count lines in a file ?

Chris Liechti cliechti at gmx.net
Tue Jul 23 13:06:32 EDT 2002


"Shagshag13" <shagshag13 at yahoo.fr> wrote in
news:ahk1nk$3v7tf$1 at ID-146704.news.dfncis.de: 

> 
> "Chris Liechti" <cliechti at gmx.net> a écrit dans le message de news:
> Xns9254BFA8ABAABcliechtigmxnet at 62.2.16.82... 
>> "Shagshag13" <shagshag13 at yahoo.fr> wrote in
>> news:ahk020$tl4ue$1 at ID-146704.news.dfncis.de:
>>
>> > i need to count lines in a file (that i *can't* keep in memory, so
>> > can't use readlines())
>>
>> but you can use xreadlines, or a bit nicer in Py2.2 where "file" is
>> an iterator.
> 
> i use file because i read this in documentation :
> 
> open(filename[, mode[, bufsize]])
> An alias for the file() function above.
> 
> does xreadlines will speed my function ?

xreadlines does read lines on demand, instead of loading the entire file.
(like range and xrange). and yes "file is open" i think it was introduced
because of the "for line in file(...)" idiom, it sound better so than with 
open...

you don't need "wc" or "wc -l" when you use the bit i posted before.

chris
-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list