Advertisement
Guest User

ReadDirectoryChangesW

a guest
May 31st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. """ Watched Folder or Hot Folder.
  2. Observes a folder for file changes, e.g. new/edited/deleted files.
  3.  
  4. inspired by: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
  5. """
  6.  
  7. import os
  8. import threading
  9.  
  10. import win32file
  11. import win32con
  12.  
  13. # actions
  14. ACTIONS = {
  15.   1 : "Created",
  16.   2 : "Deleted",
  17.   3 : "Updated",
  18.   4 : "Renamed from something",
  19.   5 : "Renamed to something"
  20. }
  21.  
  22. # constants
  23. FILE_LIST_DIRECTORY = 0x0001
  24.  
  25. class WatchedFolder:
  26.     def __init__(self, path):
  27.         self.path = path
  28.         """ path to watch """
  29.         self.buffer = 1024
  30.         """ buffer size for results """
  31.         self.thread = None
  32.         """ main thread """
  33.         self.stopped = None
  34.         """ set to True to stop watching """
  35.         self.subDirs = True
  36.         """ set to True to watch sub dirs """
  37.         self.filter = win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
  38.             win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
  39.             win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
  40.             win32con.FILE_NOTIFY_CHANGE_SIZE |
  41.             win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
  42.             win32con.FILE_NOTIFY_CHANGE_SECURITY
  43.         """ event filter """
  44.         # directory handle
  45.         self.handle = win32file.CreateFile(
  46.             path,
  47.             FILE_LIST_DIRECTORY,
  48.             win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
  49.             None,
  50.             win32con.OPEN_EXISTING,
  51.             win32con.FILE_FLAG_BACKUP_SEMANTICS,
  52.             None
  53.         )
  54.  
  55.     # run loop
  56.     def run(self):
  57.         while not self.stopped:
  58.             results = win32file.ReadDirectoryChangesW(
  59.                 self.handle,
  60.                 self.buffer,
  61.                 self.subDirs,
  62.                 self.filter,
  63.                 None,
  64.                 None
  65.             )
  66.             self.do(results)
  67.    
  68.     # start watching
  69.     def start(self):
  70.         self.stopped = False
  71.         thread = threading.Thread(target=self.run)
  72.         thread.daemon = True
  73.         thread.start()
  74.         self.thread = thread
  75.  
  76.     # stop watching
  77.     def stop(self):
  78.         self.stopped = True
  79.  
  80.     # overwrite this
  81.     def do(self, results):
  82.         for action, item in results:
  83.             fullFilename = os.path.join(self.path, item)
  84.             print fullFilename, ACTIONS.get(action, "Unknown")
  85.  
  86. def demo():
  87.     import time
  88.     path = os.path.abspath("watch")
  89.     wf = WatchedFolder(path)
  90.     wf.start()
  91.     wait = 10 # wait x seconds
  92.     print "now wait %d seconds..." % wait
  93.     time.sleep(wait)
  94.     print "stop!"
  95.     wf.stop()
  96.     print "stopped"
  97.     # hint: after stopping, the last initiated run still is alive
  98.  
  99. if __name__ == "__main__":
  100.     demo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement