Removing IS watched folder is possible in ReadDirectoryChangesW
"Understanding ReadDirectoryChangesW - Part 2" by Jim Beveridge (as Artemug mentioned) is a very good background for this problem, but the expression explaining Using FILE_SHARE_DELETE is misleading.
My tests, using FILE_SHARE_DELETE actually allows you to delete / rename the viewed folder. (In other words, you do not need to โwatch the parent folderโ as the only option.)
Here's a working snippet (edited and heavily borrowed from this excellent Tim Watch Gold Change Catalog)
# License is same as snippets on this page # http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html # In other words, bug Tim Golden to publish a license for his snippets def windows_watch_path(watched_path): import win32file import win32con ACTIONS = { 1 : "Created", 2 : "Deleted", 3 : "Updated", 4 : "RenamedFrom", 5 : "RenamedTo" } # Thanks to Claudio Grondi for the correct set of numbers FILE_LIST_DIRECTORY = 0x0001 try: hDir = win32file.CreateFile ( watched_path , FILE_LIST_DIRECTORY , win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE , None , win32con.OPEN_EXISTING , win32con.FILE_FLAG_BACKUP_SEMANTICS , None ) except: # either it does not exist by this time, or some other issue... blah. # we'll just say "it 'changed' from 'some other expected state'" return [[watched_path, '', ACTIONS[2]]] results = win32file.ReadDirectoryChangesW ( hDir, 1024, True, 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, None, None ) files_changed = [] for action, fn in results: files_changed.append( [ watched_path , fn , ACTIONS[action] ] ) # print fullfn, ACTIONS.get(action, "Unknown") return files_changed
source share