[Tutor] Closing file objects when object is garbage collected?

Wesley Brooks wesbrooks at gmail.com
Tue Feb 12 09:36:14 CET 2008


Dear Python Users,

How do I ensure that when an object is deleted by the garbage
collector that the file objects contained within the object are
closed, or collected by the garbage collector?

I'd like to avoid having to read the whole file object into a string
and close the file immediately because the files can potentially be
large, and I'm dealing with many files at a time.

I'm having issues when I test my software on XP, but not Linux. When I
run the progam it fails after running for a while but not at exactly
the same point each time. It fails in one of two ways, the user
interface either completely disappears, or gives a OS error message
unhanded exception error.

Thanks in advance of any help,

Wesley Brooks.


More information about the Tutor mailing list