windows 下和linux下 用python监控文件夹内容变化

来源:互联网 发布:小米3破解4g网络 编辑:程序博客网 时间:2024/05/16 19:10

windows 下 使用 win32API

import osimport win32fileimport win32con ACTIONS = {   1 : "Created",   2 : "Deleted",   3 : "Updated",   4 : "Renamed from something",   5 : "Renamed to something" } FILE_LIST_DIRECTORY = win32con.GENERIC_READ | win32con.GENERIC_WRITE path_to_watch = "." 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 ) if __name__ == '__main__':    while 1:        results = win32file.ReadDirectoryChangesW (                                                hDir,  #handle: Handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right.                                                1024,  #size: Size of the buffer to allocate for the results.                                                True,  #bWatchSubtree: Specifies whether the ReadDirectoryChangesW function will monitor the directory or the directory tree.                                                 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 (path_to_watch, file)             print (full_filename, ACTIONS.get (action, "Unknown"))

linux下使用pyinotify库

[python] view plaincopyprint?    #!/usr/bin/env python      #coding=utf-8      import os      import gtk      import gobject      from  pyinotify import  WatchManager, Notifier, ProcessEvent, ThreadedNotifier, IN_DELETE, IN_CREATE,IN_MOVED_TO,IN_MOVED_FROM      class hechao(ProcessEvent):          def process_IN_CREATE(self, event):              print   "创建文件: %s "  %   os.path.join(event.path, event.name)          def process_IN_DELETE(self, event):              print   "删除文件: %s "  %   os.path.join(event.path, event.name)          def process_IN_MOVED_TO(self, event):              print   "移来文件: %s "  %   os.path.join(event.path, event.name)          def process_IN_MOVED_FROM(self, event):              print   "移走文件: %s "  %   os.path.join(event.path, event.name)      path = "/home/hechao/.gnomenu/favorites"      gobject.threads_init()      wm = WatchManager()       mask = IN_DELETE|IN_CREATE|IN_MOVED_TO|IN_MOVED_FROM      notifier = ThreadedNotifier(wm, hechao())      wm.add_watch(path, mask,rec=True)      notifier.start()      def __quit(widget):      notifier.stop()      gtk.main_quit()      if __name__ == "__main__":          win = gtk.Window()          win.connect("destroy",__quit)          win.show()          gtk.main()  
0 0