How do I watch a file for changes?

前端 未结 25 2802
孤街浪徒
孤街浪徒 2020-11-21 07:13

I have a log file being written by another process which I want to watch for changes. Each time a change occurs I\'d like to read the new data in to do some processing on it

25条回答
  •  悲&欢浪女
    2020-11-21 07:58

    ACTIONS = {
      1 : "Created",
      2 : "Deleted",
      3 : "Updated",
      4 : "Renamed from something",
      5 : "Renamed to something"
    }
    FILE_LIST_DIRECTORY = 0x0001
    
    class myThread (threading.Thread):
        def __init__(self, threadID, fileName, directory, origin):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.fileName = fileName
            self.daemon = True
            self.dir = directory
            self.originalFile = origin
        def run(self):
            startMonitor(self.fileName, self.dir, self.originalFile)
    
    def startMonitor(fileMonitoring,dirPath,originalFile):
        hDir = win32file.CreateFile (
            dirPath,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
        )
        # Wait for new data and call ProcessNewData for each new chunk that's
        # written
        while 1:
            # Wait for a change to occur
            results = win32file.ReadDirectoryChangesW (
                hDir,
                1024,
                False,
                win32con.FILE_NOTIFY_CHANGE_LAST_WRITE,
                None,
                None
            )
            # For each change, check to see if it's updating the file we're
            # interested in
            for action, file_M in results:
                full_filename = os.path.join (dirPath, file_M)
                #print file, ACTIONS.get (action, "Unknown")
                if len(full_filename) == len(fileMonitoring) and action == 3:
                    #copy to main file
                    ...
    

提交回复
热议问题