how to count lines in a file ?

Shagshag13 shagshag13 at yahoo.fr
Tue Jul 23 13:27:39 EDT 2002


here is a test in counting [6871785] lines from a file of > 1Go

---
the code i post

 [Eof processing] : 00:01:13

---
the code i post without "wc popen" lines

 [Eof processing] : 00:01:09

---
lineCount = 0
fhi = file(filename)
for line in xreadlines.xreadlines(fhi):
 lineCount += 1

return lineCount

 [Eof processing] : 00:01:22
---
lines = 0:
for line in file(filename):
     lines += 1

 [Eof processing] : 00:01:22
---
lineCount = 0
fp = open(filename)
it = iter(fp)
try:
 while it.next():
  lineCount += 1
except StopIteration:
 pass

return lineCount

 [Eof processing] : 00:01:42

************

all comments are welcome !!!

thanks,

s13.





More information about the Python-list mailing list