执行多行搜索

时间:2018-07-21 16:39:06

标签: python-3.x

我当前的脚本正在运行(请参见下文),但单行匹配。如何匹配多行?

for filename in os.listdir(path):
    with open(os.path.join(path,filename), "r") as infile:
        check1 = "fail"
        check2 = "fail"
        for line in infile:
            if line.startswith("logging buffered 16384"):
                check1 = 'pass'
            if line.startswith("logging console informational"):
                check2 = 'pass'
        fh.write("{},{},{}\n".format(filename, check1, check2))
fh.close()

1 个答案:

答案 0 :(得分:0)

如果result中的所有项目都在checks

中,则变量file的值为True。
checks = ["line1", "line2"]
file = ["line3", "line4", line5", "line1", "line2"]
result = (all([True for line in checks if line in file]))

将要搜索的行定义为列表,然后在文件上运行以上代码。