Deleting files and folders used by other processes on Windows

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


tkondal at gmail.com 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:

                 try:
                     # Make the file's attributes normal so file can be 
deleted.
                     win32api.SetFileAttributes(os.path.join(root, f), 
win32con.FILE_ATTRIBUTE_NORMAL)
                     # HKLM/SYSTEM/CurrentControlSet/Control/Session 
Manager/PendingFileRenameOperations
                     win32api.MoveFileEx(os.path.join(root, f), None, 
win32con.MOVEFILE_DELAY_UNTIL_REBOOT)
                 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