以下代码使用 Python 查找包含特定关键字 'BIT_log' 和 '.log' 的日志文件。

import os

folder_path = 'C:\'
keyword1 = 'BIT_log'
keyword2 = '.log'

for filename in os.listdir(folder_path):
    if filename.endswith('.log'):
        file_path = os.path.join(folder_path, filename)
        with open(file_path, 'r') as file:
            content = file.read()
            if keyword1 in content and keyword2 in content:
                print(f'File '{filename}' contains both '{keyword1}' and '{keyword2}' keywords.')
                # perform further actions as required

该代码首先指定了文件夹路径和要查找的两个关键字。然后,使用 os.listdir 函数遍历文件夹中的所有文件。对于每个以 '.log' 结尾的文件,它打开文件并读取内容。如果文件内容中包含两个关键字,则输出文件名,并可以执行进一步的操作。

您可以修改代码中的文件夹路径、关键字和进一步的操作,以满足您的具体需求。

Python 代码:查找包含特定关键字的日志文件

原文地址: https://www.cveoy.top/t/topic/nVpm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录