how to count lines in a file ?

Chris Liechti cliechti at gmx.net
Tue Jul 23 12:49:26 EDT 2002


"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.

lines = 0:
for line in file("countme.txt"):
    	lines += 1

(you can substitute file(...) with open(...).xreadlines() in older Py's)

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list