Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using Python 2.7. I want to delete a folder which may or may not be empty. The folder is handled by a thread for file-monitoring. I am not able to kill the thread, but wanted to delete this folder anyhow. I tried with:

os.rmdir(Location)
shutil.rmtree(Location) 
os.unlink(Location)

...but, it didn't work. It is showing this error:
[Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\cipher~1\\appdata\\local\\temp\\fis\\a0c433973524de528420bbd56f8ede609e6ea700'
I want to delete folder a0c433973524de528420bbd56f8ede609e6ea700 or delete whole path will also suffice.

class myThread (threading.Thread):
    def __init__(self, threadID, fileName, directory, origin):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.fileName = fileName
        self.daemon = True
        self.dir = directory
        self.originalFile = origin
    def run(self):
        startMonitor(self.fileName, self.dir, self.originalFile)

def startMonitor(fileMonitoring,dirPath,originalFile):
    logging.debug("in startMonitor")
    hDir = win32file.CreateFile (
      dirPath,
      FILE_LIST_DIRECTORY,
      win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
      None,
      win32con.OPEN_EXISTING,
      win32con.FILE_FLAG_BACKUP_SEMANTICS,
      None
    )
    logging.debug("Wait for new data and call ProcessNewData for each new chunk that's written")
    readFlags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME  | \
            win32con.FILE_NOTIFY_CHANGE_DIR_NAME   | \
            win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | \
            win32con.FILE_NOTIFY_CHANGE_SIZE       | \
            win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | \
            win32con.FILE_NOTIFY_CHANGE_SECURITY
    # Wait for new data and call ProcessNewData for each new chunk that's written
    while 1:
        # Wait for a change to occur
        results = win32file.ReadDirectoryChangesW (
                                                   hDir,
                                                   1024,
                                                   False,
                                                   readFlags,
                                                   None
                                                   )
        # For each change, check to see if it's updating the file we're interested in
        logging.debug("For each change, check to see if it's updating the file we're interested in")
        for action, file_M in results:
            full_filename = os.path.join (dirPath, file_M)
            #print file, ACTIONS.get (action, "Unknown")
            if len(full_filename) == len(fileMonitoring) and action == 3:
                #copy to main file
                if os.path.exists(originalFile):
                        encrypt_file(key,fileMonitoring,originalFile,iv)

 try:
        thread1 = myThread(1, FileName, Location,selectedFileName)
        thread1.start();
        startupinfo = None
        if os.name == 'nt':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
            logging.debug("control to file open subprocess")
            ss=subprocess.Popen(FileName,shell=True)
            ss.communicate()


            logging.debug("file open subprocess executed")
            removeTempFile(FileName)
            logging.debug("file removed")
            shutil.rmtree(Location) #to remove folder, I asked question for this ony.
            sys.exit(0)
    except Exception as e:
        print e
        logging.error(e)
        logging.debug("exception in encryption Thread")
        removeTempFile(FileName)
        sys.exit(e)
share|improve this question

1 Answer 1

Windows uses locking. You cannot delete a file or folder that is in use by another process. Some kind of locks affect other threads within the same process too. I fear you'll have to terminate the monitoring thread or divert it to another directory before you can delete this directory.

share|improve this answer
    
Please see the code –  sherlock Nov 6 '13 at 4:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.