how to count lines in a file ?
William Park
opengeometry at NOSPAM.yahoo.ca
Tue Jul 23 15:07:15 EDT 2002
Shagshag13 <shagshag13 at yahoo.fr> wrote:
> 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.
If you are on Unix, then just use
wc -l filename
with any of function in 'os' module.
--
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin
More information about the Python-list
mailing list