how to count lines in a file ?

Bo M. Maryniuck b.maryniuk at forbis.lt
Thu Jul 25 04:24:42 EDT 2002


On Thursday 25 July 2002 02:15, Richard Jones wrote:
> As far as the community is concerned, the file is explicitly closed because
> no reference to it is retained. No, this is not documented. Then again, the
> documentation makes no claims about any sort of behaviour when the object
> is collected. The community has, reasonably enough I think, always assumed
> (for a very long time in many public ways) that the file is closed when it
> is collected! As to Jython not closing the file - well, I'd consider that a
> bug...

Umm... Richard, I just've played over all we talking about in not best way, 
but in anyway I'm little hooked and I would like to know what's _exactly_ 
going on:

*bo at spectator:(~) python
Python 2.2 (#1, Dec 23 2001, 09:30:32) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gc
>>> len(gc.get_objects())
844
>>> a = open('/etc/passwd')
>>> len(gc.get_objects())
844
>>> del a
>>> len(gc.get_objects())
844
>>> #Huh?
>>>
>>> a = open('/etc/passwd').readlines()
>>> len(gc.get_objects())
871
>>> #Yeppiie!
>>>
>>> del a
>>> len(gc.get_objects())
870
>>> a = open('/etc/passwd')            
>>> len(gc.get_objects())
870
>>> a.close()
>>> len(gc.get_objects())
870
>>> a
<closed file '/etc/passwd', mode 'r' at 0x8164308>
>>> del a
>>> len(gc.get_objects())
870
>>> #Huh?

-- 
Sincerely yours, Bogdan M. Maryniuck

"How should I know if it works?  That's what beta testers are for.  I only
coded it."
(Attributed to Linus Torvalds, somewhere in a posting)





More information about the Python-list mailing list