how to count lines in a file ?

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Jul 23 12:35:48 EDT 2002


On 23-Jul-2002 Shagshag13 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()
> 

read() copies the whole chunk into memory as well so you might as well use
readlines().  otherwise just use a standard loop and counter.





More information about the Python-list mailing list