I am trying to watch a directory for create/delete/rename changes on windows with python using the ReadDirectoryChangesW API. This is my code and it is working fine:
results = win32file.ReadDirectoryChangesW(self.hDir, 8192, True, self.type, None,
None)
for action, file in results:
full_filename = os.path.join (self.source_path, file)
if action == 1: # Created
self.fileCreated(full_filename)
elif action == 2: # Deleted
self.fileDeleted(full_filename)
elif action == 3: # Updated
self.fileUpdated(full_filename)
elif action == 4: # Renamed from something
renamed_file = full_filename
elif action == 5: # Renamed to something
self.fileRenamed(renamed_file, full_filename)
However, when I try to delete the watched folder from python or from windows explorer, I get:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\users\user\appdata\local\temp\new_dir'
I believe this makes sense, but how should I solve this? Because my application should allow the user to remove a watched folder. I tried the solution for the asynchronous method http://www.themacaque.com/?p=859, but it didn't help.
Thanks in advance!
From this blog post:
Another potential pitfall of [ReadDirectoryChangesW] is that the referenced directory itself is now "in use" and so can't be deleted. To monitor files in a directory and still allow the directory to be deleted, you would have to monitor the parent directory and its children.
The post also provides some more details on proper use of ReadDirectoryChangesW