天天看点

python seek、re.search实现文件新增内容监控、关键字检索

脚本实现了一下功能:

  1. GetFileEndChange方法实现了对文件追加内容的监控
  2. GetFileKeyInfo方法实现了对文件关键字的检索
  3. GetFileNewKeyData方法实现了对文件追加的内容关键字的检索
  4. 对交互的优化,实现了检索文件、检索次数的统计

应用场景:

日志文件管理

脚本

#coding:utf-8
"""
Note:
    获取文件的改变
Author:redsun 
Data:2019/7/10
"""
import re
import time
import os
def GetFileEndChange(File_Path):
    '''
    从文件尾部开始检查文件追加的内容
    :param File_Path: 路径
    :return: 输出尾部增加的内容
    '''
    with open( File_Path ) as f:
        f.seek(0,2)
        cur = f.tell()
        temp = 0
        text = ''
        HelpInfoHead('Change', File_Path, cur)
        while True:
            f.seek(cur)
            ch = f.readlines()
            # 如果没有读到数据,跳出循环
            if not ch:
                if HelpInfoEnd('Change',temp) == 'no':
                    break
            else:
                for line in ch:
                    text += line
                HelpInfoContent(text, temp)
            temp += 1
            cur = f.tell()

def GetFileKeyInfo(File_Path, KeyWord):
    '''
    检索文件中与关键字相关的行,并输出
    :param File_Path: 文件路径
    :param KeyWord: 关键字
    :return: 输出关键字所在的行
    '''
    with open(File_Path) as f:
        lines = f.readlines()
        if len(lines) == 0:
            print("================日志文件为空================")
        else:
            count = 0
            text = ''
            HelpInfoHead('KeyInfo', File_Path)
            for line in lines:
                rs = re.search(KeyWord, line)
                if rs:
                    count += 1
                    text += line
            HelpInfoContent(text)
            print('[命中{count}次]'.format(count=count))
            HelpInfoEnd('KeyInfo')



def GetFileNewKeyData(File_Path, KeyWord):
    '''
    检索文件尾部新增内容关键字匹配,并输出结果
    :param File_Path: 文件路径
    :param KeyWord: 关键字
    :return:
    '''
    with open(File_Path) as f:
        f.seek(0, 2)
        cur = f.tell()
        temp = 0
        count = 0
        text = ''
        HelpInfoHead('NewKeyData', File_Path, cur)
        while True:
            f.seek(cur)
            ch = f.readlines()
            # 如果没有读到数据,跳出循环
            if not ch:
                if HelpInfoEnd('NewKeyData', temp) == 'no':
                    break
            else:
                for line in ch:
                    rs = re.search(KeyWord, line)
                    if rs:
                        count += 1
                        text += line
                HelpInfoContent(text)
                print('[命中{count}次]'.format(count=count))

            temp += 1
            cur = f.tell()

def HelpInfoHead(FuctionName, File_Path, bits = -1):
    '''
    提示信息——头部
    :param FuctionName:函数名 
    :param file_name: 文件名
    :param bits: 当前文件结束地址
    :return: 
    '''
    file_name = os.path.basename(File_Path)
    print(FuctionName)
    print('----------------------------------------------------------')
    print('Start Listen The File ({file_name}) Info ...'.format(file_name=file_name))
    if bits != -1:
        print('The File Ends in {bits} bits . '.format(bits=bits))
    print('//')
    
def HelpInfoEnd(FuctionName,times=-1):
    '''
    提示信息——尾部
    :return: no 结束 yes 继续
    '''
    if times == 0:
        raw = input('Wait Code (not "no") To Start Check:')
    elif times == -1 :
        raw = 'no'
    else:
        print('Check {times} times. '.format(times=times))
        raw = input('Continue Check The File Key Info (yes/no):')
    print('No {FuctionName} in {clock}'.format(FuctionName= FuctionName, clock = GetTime()) )

    if raw == 'no' or raw == 'NO':
        print('check finish !')
        print('----------------------------------------------------------')
        sign = 'no'
    else:
        sign = 'yes'

    return sign

def HelpInfoContent(text, times = 0):
    print('**********************************************************')
    if times != 0:
        print('Check {times} times. '.format(times=times))
    print('Changed at {times} content: \n {text}'.format(
        times=GetTime(), text=text))
    # count = 1
    # for line in text:
    #     print('{count} \t {line}'.format(count = count, line = line))
    #     count += 1
    print('**********************************************************')
    
def GetTime():
    '''
    返回当前时间
    :return: 
    '''
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

           

效果:

python seek、re.search实现文件新增内容监控、关键字检索