我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用win32file.ReadDirectoryChangesW()。
def _watcherThread(self, dn, dh, changes): # A synchronous version: # XXX - not used - I was having a whole lot of problems trying to # get this to work. Specifically: # * ReadDirectoryChangesW without an OVERLAPPED blocks infinitely. # * If another thread attempts to close the handle while # ReadDirectoryChangesW is waiting on it, the ::CloseHandle() method # blocks (which has nothing to do with the GIL - it is correctly # managed) # Which ends up with no way to kill the thread! flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME while 1: try: print "waiting", dh changes = win32file.ReadDirectoryChangesW(dh, 8192, False, #sub-tree flags) print "got", changes except: raise changes.extend(changes)
def tearDown(self): # be careful about raising errors at teardown! for h in self.dir_handles: # See comments in _watcherThread above - this appears to # deadlock if a synchronous ReadDirectoryChangesW is waiting... # (No such problems with an asynch ReadDirectoryChangesW) h.Close() for dn in self.dir_names: try: shutil.rmtree(dn) except OSError: print "FAILED to remove directory", dn for t in self.watcher_threads: # closing dir handle should have killed threads! t.join(5) if t.isAlive(): print "FAILED to wait for thread termination"
def _watcherThread(self, dn, dh, changes): # A synchronous version: # XXX - not used - I was having a whole lot of problems trying to # get this to work. Specifically: # * ReadDirectoryChangesW without an OVERLAPPED blocks infinitely. # * If another thread attempts to close the handle while # ReadDirectoryChangesW is waiting on it, the ::CloseHandle() method # blocks (which has nothing to do with the GIL - it is correctly # managed) # Which ends up with no way to kill the thread! flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME while 1: try: print("waiting", dh) changes = win32file.ReadDirectoryChangesW(dh, 8192, False, #sub-tree flags) print("got", changes) except: raise changes.extend(changes)
def tearDown(self): # be careful about raising errors at teardown! for h in self.dir_handles: # See comments in _watcherThread above - this appears to # deadlock if a synchronous ReadDirectoryChangesW is waiting... # (No such problems with an asynch ReadDirectoryChangesW) h.Close() for dn in self.dir_names: try: shutil.rmtree(dn) except OSError: print("FAILED to remove directory", dn) for t in self.watcher_threads: # closing dir handle should have killed threads! t.join(5) if t.isAlive(): print("FAILED to wait for thread termination")
def watchos(): #get path or maintain current path of app FILE_LIST_DIRECTORY = 0x0001 try: path_to_watch = myos.get() or "." except: path_to_watch = "." path_to_watch = os.path.abspath(path_to_watch) textbox.insert(END, "Watching %s at %s" % (path_to_watch, time.asctime()) + "\n\n") # FindFirstChangeNotification sets up a handle for watching # file changes. while 1: hDir = win32file.CreateFile ( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None ) change_handle = win32file.ReadDirectoryChangesW ( hDir, 1024, True,#Heap Size include_subdirectories, 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 ) # Loop forever, listing any file changes. The WaitFor... will # time out every half a second allowing for keyboard interrupts # to terminate the loop. ACTIONS = { 1 : "Created", 2 : "Deleted", 3 : "Updated", 4 : "Renamed from something", 5 : "Renamed to something" } results = change_handle for action, files in results: full_filename = os.path.join(path_to_watch, files) theact = ACTIONS.get(action, "Unknown") textbox.insert(END, str(full_filename) + "\t" + str(theact) +"\n")
def run(self): if running_on_linux()==True: print("thread: start") wm = pyinotify.WatchManager() print("wathcing path",self.watch_path) ret=wm.add_watch(self.watch_path, pyinotify.IN_CLOSE_WRITE, self.onChange,False,False) print(ret) print("thread: start notifyer",self.notifier) self.notifier = pyinotify.Notifier(wm) try: while 1: self.notifier.process_events() if self.notifier.check_events(): self.notifier.read_events() #self.notifier.loop() except: print("error in notify",sys.exc_info()[0]) else: hDir = win32file.CreateFile (self.watch_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) while 1: 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) for action, file in results: full_filename = os.path.join (self.watch_path, file) self.onChange(full_filename)
def _watcherThreadOverlapped(self, dn, dh, changes): flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME buf = win32file.AllocateReadBuffer(8192) overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) while 1: win32file.ReadDirectoryChangesW(dh, buf, False, #sub-tree flags, overlapped) # Wait for our event, or for 5 seconds. rc = win32event.WaitForSingleObject(overlapped.hEvent, 5000) if rc == win32event.WAIT_OBJECT_0: # got some data! Must use GetOverlappedResult to find out # how much is valid! 0 generally means the handle has # been closed. Blocking is OK here, as the event has # already been set. nbytes = win32file.GetOverlappedResult(dh, overlapped, True) if nbytes: bits = win32file.FILE_NOTIFY_INFORMATION(buf, nbytes) changes.extend(bits) else: # This is "normal" exit - our 'tearDown' closes the # handle. # print "looks like dir handle was closed!" return else: print "ERROR: Watcher thread timed-out!" return # kill the thread!
def _watcherThreadOverlapped(self, dn, dh, changes): flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME buf = win32file.AllocateReadBuffer(8192) overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) while 1: win32file.ReadDirectoryChangesW(dh, buf, False, #sub-tree flags, overlapped) # Wait for our event, or for 5 seconds. rc = win32event.WaitForSingleObject(overlapped.hEvent, 5000) if rc == win32event.WAIT_OBJECT_0: # got some data! Must use GetOverlappedResult to find out # how much is valid! 0 generally means the handle has # been closed. Blocking is OK here, as the event has # already been set. nbytes = win32file.GetOverlappedResult(dh, overlapped, True) if nbytes: bits = win32file.FILE_NOTIFY_INFORMATION(buf, nbytes) changes.extend(bits) else: # This is "normal" exit - our 'tearDown' closes the # handle. # print "looks like dir handle was closed!" return else: print("ERROR: Watcher thread timed-out!") return # kill the thread!
def run(self,**kwargs): ''' sits and waits for a directory change. on change: 1. reads current db monitor list 2. if file is being monitored, copies file zipped to backup dir 3. queues upload job ''' while True: results = win32file.ReadDirectoryChangesW( self.hDir, 16384, 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) if kwargs.get('function'): kwargs.get('function')() for action, file in results: full_filename = os.path.join(self.path_to_watch,file) monitor_comparison = check_monitor(full_filename) if len(monitor_comparison) == 1: try: outcome = check_md5_and_process(monitor_comparison,full_filename,self.backup_dir) if outcome == True: if kwargs.get('signal'): kwargs.get('signal').emit() except FileNotFoundError: pass