how to count lines in a file ?

Shagshag13 shagshag13 at yahoo.fr
Wed Jul 24 10:41:12 EDT 2002


"Alex Martelli" <aleax at aleax.it> a écrit dans le message de news: MTy%8.131876$vm5.4340949 at news2.tin.it...
> Bo M. Maryniuck wrote:
>         ...
> >> >  for line in open(filename):
> >> >      count += 1
> >
> > In anyway this example probably does not work: looping over not sequence
> > and you probably should use xreadlines().
>
> Not with today's Python, 2.2.* -- looping over files and dicts is
> perfectly well supported today (and since 7 months ago)...

so and definitively which one should i prefer ?

this :
for line in open(filename):
    count += 1

or this :
fhi = file(filename)
while 1:
     lines = fhi.readlines(bufferSize)
     if not lines:
        break
     lineCount += len(lines)

thanks,

s13.









More information about the Python-list mailing list