Deleting files and folders used by other processes on Windows

brad byte8bits at
Wed Jul 18 22:09:55 CEST 2007

tkondal at wrote:
> Hi,
> I have been looking into making my file cleaning script more
> intelligent.  The goal of the script is to delete everything on a
> drive except for a couple of folders which are skipped by the script.
> Recently, I noticed that some files where not being deleted because a
> process was using them.  

Try this:

                     # Make the file's attributes normal so file can be 
                     win32api.SetFileAttributes(os.path.join(root, f), 
                     # HKLM/SYSTEM/CurrentControlSet/Control/Session 
                     win32api.MoveFileEx(os.path.join(root, f), None, 
                 except Exception, e:
                     print e

Upon reboot the file will be gone... careful though, this canl delete 
any Windows system file too.

More information about the Python-list mailing list