Python是一个可以由另一个进程重命名的日志文件

2024-05-16 04:15:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个将数据记录到日志文件的应用程序(数据.log). 此文件限制为2 MB。到达cap后,它将重命名为备份文件(data.log.bak文件)并创建一个同名的新日志文件(数据.log)开始在那里登陆。如果到达新文件的上限,它会再次将其保存到data.log.bak文件旧的日志文件被删除。你知道吗

我需要写一个python2.7脚本,在日志中寻找一个特定的字符串。它应该与主应用程序并行执行,以说明删除的bak文件。脚本应该在PC和Mac上工作。你知道吗

如何处理读取被重命名/删除的文件。 我使用了一些代码:https://stackoverflow.com/a/39213830/769078

def open_file(self):
    if sys.platform == 'win32':
        # get an handle using win32 API, specifying the SHARED access!
        handle = win32file.CreateFile(self.filename,
                                      win32file.GENERIC_READ,
                                      win32file.FILE_SHARE_DELETE |
                                      win32file.FILE_SHARE_READ |
                                      win32file.FILE_SHARE_WRITE,
                                      None,
                                      win32file.OPEN_EXISTING,
                                      0,
                                      None)
        # detach the handle
        detached_handle = handle.Detach()
        # get a file descriptor associated to the handle\
        file_descriptor = msvcrt.open_osfhandle(detached_handle, os.O_RDONLY)
        # open the file descriptor
        f = os.fdopen(file_descriptor)
    else:
        f = open(self.filename, 'r')

    return f


def follow(self):
    if not os.path.isfile(self.filename):
        return False

    current = self.open_file()
    try:
        ino = os.fstat(current.fileno()).st_ino
        while True:
            line = current.readline().strip()
            where = current.tell()
            print '[log]', line
            if line:  # Read a line
                if 'error' in line:  # If error found in line
                    self.found = True
                    print '[Debug] Found exception'
                    break
            elif self.stop:  # no new line and no new file. Stop reading if not stop
                print '[Debug] Stopping'
                break
            elif os.stat(self.filename).st_ino != ino:  # Reached end of file. Check if new file exists
                print '[Debug] Detected new file'
                new = self.open_file()
                current.close()
                current = new
                ino = os.fstat(current.fileno()).st_ino
            else:  # no new line and no new file and not stop. Sleep for a second
                print '[Debug] sleeping'
                current.seek(where)
                time.sleep(2)
    finally:
        # close the file
        current.close()

我使用线程与主应用程序并行运行脚本。我跳过了这里的代码。你知道吗

*更新*

使用ino对我没用。我可能弄错了。相反,我依靠的是修改后的时间。你知道吗

def follow(self):
    if not os.path.isfile(self.filename):
        return False

    current = self.open_file()
    bak_modified_time = None
    try:
        while True:
            line = current.readline().strip()
            where = current.tell()
            if line:  # Read a line
                if 'error' in line:  # If error found in line
                    self.found = True
                    print '[Debug] Found exception'
                    break
            elif self.stop:  # no new line and no new file. Stop reading if not stop
                print '[Debug] Stopping'
                break
            elif ((not bak_modified_time and os.path.exists(self.bak_filename)) or
                  (bak_modified_time and os.path.exists(self.bak_filename) and
                   os.stat(self.bak_filename).st_mtime != bak_modified_time))
                print '[Debug] Detected new file'
                new = self.open_file()
                current.close()
                current = new
                bak_modified_time = os.stat(self.bak_filename).st_mtime
            else:  # no new line and no new file and not stop. Sleep for a second
                print '[Debug] sleeping'
                current.seek(where)
                time.sleep(2)
    finally:
        # close the file
        current.close()

Tags: and文件nodebugselfnewifos