will it cause any problems to open a read-only file & not close it?

Daniel Dittmar daniel.dittmar at sap.corp
Mon Mar 14 14:45:13 CET 2005

Fuzzyman wrote:
> Sara Khalatbari wrote:
>>Will it cause any problems if you open a file to read
>>& never close it?
> Under CPython the filehandle will be automatically garbage collected.
> Under JPython (Jython) it won't be... 

Isn't it rather that CPython will close the file as soon as the last 
reference is dropped, which is probably right after the read returns?

Whereas Jython/Java will close the file, when the garbage collector 
actually reclaims the object, which will be later. Or much later.

 > It's a very useful shortcut
 > though *sigh*

It isn't that difficult to write a small function to that effect.

It would perhaps be wise to acknowledge the existence of Jython and 
IronPython by allowing an additional flag for open to the effect "close 
when the end of the file is reached". This would prevent similar 
problems with iter (open (...)), which can't be wrapped quite as easily.


More information about the Python-list mailing list