I created a python script that gets a list of files from a text file and deletes them if they are empty. It correctly detects empty files, but does not want to delete them. This gives me:
(32, 'The process cannot access the file because it is being used by another process')
I used two different tools to check if the files are locked or not, and I'm sure it is not. I used sysinternals process explorer and LockHunter. Alternatively, I can simply manually delete the files myself. Obviously, I do not want to do this for everyone, since there are hundreds in different places.
script:
import os.path import sys def DeleteFilesFromListIfBlank(PathToListOfFiles): ListOfFiles = open(PathToListOfFiles) FilesToCheck = []; for line in ListOfFiles.readlines(): if(len(line) > 1): line = line.rstrip(); FilesToCheck.append(line) print "Found %s files to check. Starting check." % len(FilesToCheck) FilesToRemove = []; for line in FilesToCheck: #print "Opening %s" % line try: ActiveFile = open(line); Length = len(ActiveFile.read()) if(Length < 691 and ActiveFile.read() == ""): print "Deleting %s" % line os.unlink(line); else: print "Keeping %s" % line except IOError,message: print "Could not open file: $s" % message except Exception as inst: print inst.args DeleteFilesFromListIfBlank("C:\\ListOfResx.txt")
I tried using both os.unlink and os.remove. I am running Python 2.6 on Vista64
thanks
source share