Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于运行时日志分段的python脚本_Python - Fatal编程技术网

用于运行时日志分段的python脚本

用于运行时日志分段的python脚本,python,Python,我需要在运行时读取日志文件,并根据搜索将它们分离到多个不同的文件中 由于日志文件将每天轮换,我使用“getmtime”读取最新修改的日志文件,并在日志中更新时动态读取行,并将它们分离到多个文件中 但是,我的代码无法读取日志文件中的新行。在这里请求您的输入 import time import os import glob newest = max(glob.iglob('/var/log/*.log'), key=os.path.getmtime) with open(newest,'r')

我需要在运行时读取日志文件,并根据搜索将它们分离到多个不同的文件中

由于日志文件将每天轮换,我使用“getmtime”读取最新修改的日志文件,并在日志中更新时动态读取行,并将它们分离到多个文件中

但是,我的代码无法读取日志文件中的新行。在这里请求您的输入

import time
import os
import glob

newest = max(glob.iglob('/var/log/*.log'), key=os.path.getmtime)

with open(newest,'r') as file, \
open(‘result1.log’, ‘w’) as output_file1, \
open(‘result2.log’, ‘w’) as output_file2, \
open(‘result3.log’, ‘w’) as output_file3:


    while 1:

        where = file.tell()
        line = file.readline()

        if not line:

           time.sleep(1)

           file.seek(where)

        else:

             if “abc” in line:

                 output_file1.write(line)

             if “def” in line:

                 output_file2.write(line)

             if “ghi” in line:

                 output-file3.write(line)

         newest1 = max(glob.iglob('/var/log/*.log'), key=os.path.getmtime)

         if newest1 != newest

             newest= newest1

             file = open(newest, 'r')
谢谢你,再见,
Ankith

首先,您的代码包含语法错误,因此我认为您上面介绍的代码与您实际使用的代码不一样,或者您可能会注意到。我已经复制粘贴了您的样本,修复了两个错误并运行了它-结果与您预期的一样,新的行被正确读取。因此,我相信您的问题与此样本无关。

您好,谢谢您的快速回复。我同意当我从记事本中复制时可能会出现语法错误。所以你们的意思是说这段代码在填充时正在从日志中读取新行?