how to count lines in a file ?

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Wed Jul 24 19:46:49 EDT 2002


"Delaney, Timothy" <tdelaney at avaya.com> writes:
> > 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.

Unfortunately, it's apparently a documented feature that the above is
supposed to work, so really it's a unfortunate specification in Python
rather than broken code.  There's probably no way to fix it now.



More information about the Python-list mailing list