Q on explicitly calling file.close
Tim Chase
python.list at tim.thechases.com
Sat Sep 5 16:04:55 EDT 2009
> CPython uses reference counting, so an object is garbage collected as
> soon as there are no references to it, but that's just an implementation
> detail.
>
> Other implementations, such as Jython and IronPython, don't use
> reference counting, so you don't know when an object will be garbage
> collected, which means that the file might remain open for an unknown
> time afterwards in case 1 above.
>
> Most people use CPython, so it's not surprising that case 1 is so
> common.
Additionally, many scripts just use a small number of files (say,
1-5 files) so having a file-handle open for the duration of the
run it minimal overhead.
On the other hand, when processing thousands of files, I always
explicitly close each file to make sure I don't exhaust some
file-handle limit the OS or interpreter may enforce.
-tkc
More information about the Python-list
mailing list