Detecting open files and forcing closure

Steven D'Aprano steve at
Sat Jan 10 10:24:51 CET 2009

On Fri, 09 Jan 2009 22:06:03 -0500, Andrew Robert wrote:

> Is there a way to detect the open files and close them out?

You can detect open files by trying to move them and catching the 
exception when you can't. You may wish to distinguish permission errors.

As far as forcing the file to close, that depends on what OS you are 
using and what permissions your application is running as. I intuit that 
you are running under Windows, because POSIX systems allow you to copy 
open files. My knowledge of Windows isn't that great, but I imagine that 
the only way to force a close is something drastic like rebooting the 

Sounds like your real problem is a social problem, not a technical one. 
Maybe you need a "failed to log out" jar (like a swear jar), and every 
person who failed to close their open files has to put a dollar in the 


More information about the Python-list mailing list