How do I watch a file for changes?

前端 未结 25 2816
孤街浪徒
孤街浪徒 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 08:00

    This is an example of checking a file for changes. One that may not be the best way of doing it, but it sure is a short way.

    Handy tool for restarting application when changes have been made to the source. I made this when playing with pygame so I can see effects take place immediately after file save.

    When used in pygame make sure the stuff in the 'while' loop is placed in your game loop aka update or whatever. Otherwise your application will get stuck in an infinite loop and you will not see your game updating.

    file_size_stored = os.stat('neuron.py').st_size
    
      while True:
        try:
          file_size_current = os.stat('neuron.py').st_size
          if file_size_stored != file_size_current:
            restart_program()
        except: 
          pass
    

    In case you wanted the restart code which I found on the web. Here it is. (Not relevant to the question, though it could come in handy)

    def restart_program(): #restart application
        python = sys.executable
        os.execl(python, python, * sys.argv)
    

    Have fun making electrons do what you want them to do.

    0 讨论(0)
  • 2020-11-21 08:03

    Did you try using Watchdog?

    Python API library and shell utilities to monitor file system events.

    Directory monitoring made easy with

    • A cross-platform API.
    • A shell tool to run commands in response to directory changes.

    Get started quickly with a simple example in Quickstart...

    0 讨论(0)
  • 2020-11-21 08:03

    Here's an example geared toward watching input files that write no more than one line per second but usually a lot less. The goal is to append the last line (most recent write) to the specified output file. I've copied this from one of my projects and just deleted all the irrelevant lines. You'll have to fill in or change the missing symbols.

    from PyQt5.QtCore import QFileSystemWatcher, QSettings, QThread
    from ui_main_window import Ui_MainWindow   # Qt Creator gen'd 
    
    class MainWindow(QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            QMainWindow.__init__(self, parent)
            Ui_MainWindow.__init__(self)
            self._fileWatcher = QFileSystemWatcher()
            self._fileWatcher.fileChanged.connect(self.fileChanged)
    
        def fileChanged(self, filepath):
            QThread.msleep(300)    # Reqd on some machines, give chance for write to complete
            # ^^ About to test this, may need more sophisticated solution
            with open(filepath) as file:
                lastLine = list(file)[-1]
            destPath = self._filemap[filepath]['dest file']
            with open(destPath, 'a') as out_file:               # a= append
                out_file.writelines([lastLine])
    

    Of course, the encompassing QMainWindow class is not strictly required, ie. you can use QFileSystemWatcher alone.

    0 讨论(0)
  • 2020-11-21 08:03

    I'd try something like this.

        try:
                f = open(filePath)
        except IOError:
                print "No such file: %s" % filePath
                raw_input("Press Enter to close window")
        try:
                lines = f.readlines()
                while True:
                        line = f.readline()
                        try:
                                if not line:
                                        time.sleep(1)
                                else:
                                        functionThatAnalisesTheLine(line)
                        except Exception, e:
                                # handle the exception somehow (for example, log the trace) and raise the same exception again
                                raw_input("Press Enter to close window")
                                raise e
        finally:
                f.close()
    

    The loop checks if there is a new line(s) since last time file was read - if there is, it's read and passed to the functionThatAnalisesTheLine function. If not, script waits 1 second and retries the process.

    0 讨论(0)
  • 2020-11-21 08:04

    If polling is good enough for you, I'd just watch if the "modified time" file stat changes. To read it:

    os.stat(filename).st_mtime
    

    (Also note that the Windows native change event solution does not work in all circumstances, e.g. on network drives.)

    import os
    
    class Monkey(object):
        def __init__(self):
            self._cached_stamp = 0
            self.filename = '/path/to/file'
    
        def ook(self):
            stamp = os.stat(self.filename).st_mtime
            if stamp != self._cached_stamp:
                self._cached_stamp = stamp
                # File has changed, so do something...
    
    0 讨论(0)
  • 2020-11-21 08:05

    Here is a simplified version of Kender's code that appears to do the same trick and does not import the entire file:

    # Check file for new data.
    
    import time
    
    f = open(r'c:\temp\test.txt', 'r')
    
    while True:
    
        line = f.readline()
        if not line:
            time.sleep(1)
            print 'Nothing New'
        else:
            print 'Call Function: ', line
    
    0 讨论(0)
提交回复
热议问题