how to count lines in a file ?

Delaney, Timothy tdelaney at avaya.com
Wed Jul 24 18:31:16 EDT 2002


> 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.

Other people have already addressed the other problem in the above code.

Tim Delaney




More information about the Python-list mailing list