how to count lines in a file ?

Richard Jones rjones at ekit-inc.com
Wed Jul 24 19:36:55 EDT 2002


On Thu, 25 Jul 2002 8:31 am, Delaney, Timothy wrote:
> > From: Bo M. Maryniuck [mailto:b.maryniuk at forbis.lt]
> >
> > print len(open('/etc/passwd').readlines())
>
> There is currently discussion on python-dev of the file object possibly
> becoming collectable by GC (and hence not going away immediately the last
> reference you know about disappears).
>
> Whilst is it *extremely* unlikely that this would be allowed (due to the
> preponderance of broken code such as the above) it does point out that such
> code is indeed broken.

Sorry, I'm coming in to this thread halfway through, but I need to know why 
that code is broken? Is it the "open('/etc/passwd').readlines()"? That's a 
very common pattern, as you note, because people think it is perfectly 
legitemate. What about it is broken? Why isn't it pointed out more often that 
it _is_ broken?


   Richard





More information about the Python-list mailing list