File Closing Problem in 2.3 and 2.4, Not in 2.5

rweth rweth at
Sat Jan 6 21:44:06 CET 2007

Martin v. Löwis wrote:
> Carroll, Barry schrieb:
>> What I want to know is:
>> * has anyone else encountered a problem like this, * how was the
>> problem corrected, * can the fix be retro-fitted to 2.5 and 2.4?
> From your description, I suspect an error in your code. Your description
> indicates that you don't expect to have more than five files open
> simultaneously. Yet, the error message "Too many open files" occurs when
> you open many more files (in the order of hundreds of files).
> It is very unlikely that there is a bug in Python where it would fail to
> close a file when .close() is explicitly invoked on it (as your
> description suggests that you do), so if you get that error message, it
> can only mean that you fail to close some files.
> Notice that you may have other files open, as well, and that those also
> count towards the limit.
> As a debugging utility, you can use Sysinternal's process explorer.
> Make the program halt (not exit) when the exception occurs (e.g. by
> having it sleep(1) in a loop), then view all open handles in the
> process explorer (check the menu if it doesn't display them initially).
> Regards,
> Martin

I agree with Martin .. this code to close is solid.

Make certain you are really closing the files when you think you should. 
I am pretty sure you are not. Look at the code that closes the files 
closely. Put a print statement in the block that is supposed to close 
the files (may bee even a raw_input("closing file" + afile)  statement).

My guess is that you won't see the print statements trigger when you 
though they should .. they may be out of "the loop" you thought that 
they were in.

More information about the Python-list mailing list