Basic file operation questions

Jeff Shannon jeff at ccvcorp.com
Tue Feb 8 15:49:36 EST 2005


Marc Huffnagle wrote:

> When you read a file with that method, is there an implied close() call 
> on the file?  I assume there is, but how is that handled?
> 
[...]
>>> for line in file(...):
>>>   # do stuff

As I understand it, the disk file will be closed when the file object 
is garbage collected.  In CPython, that will be as soon as there are 
no active references to the file; i.e., in the above case, it should 
happen as soon as the for loop finishes.  Jython uses Java's garbage 
collector, which is a bit less predictable, so the file may not be 
closed immediately.  It *will*, however, be closed during program 
shutdown if it hasn't happened before then.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list