how to count lines in a file ?

Shagshag13 shagshag13 at yahoo.fr
Tue Jul 23 12:26:09 EDT 2002


hello,

i need to count lines in a file (that i *can't* keep in memory, so can't use readlines())
by now i use this :
---
import os

BUFFER_SIZE = 10000

def lineCount(filename, bufferSize = BUFFER_SIZE):

 do = 'wc ' + filename
 p = os.popen(do, 'r')
 line = p.read()
 p.close()

 if line:
  return long(line.strip().split()[0])
 else:
  lineCount = 0
  fhi = file(filename)
  while 1:
   lines = fhi.readlines(bufferSize)
   if not lines:
      break

   lineCount += len(lines)
  return lineCount
---
is it the correct ? do you think i can tweak it ?

thanks in advance,

s13.





More information about the Python-list mailing list