Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 执行多行搜索_Python 3.x - Fatal编程技术网

Python 3.x 执行多行搜索

Python 3.x 执行多行搜索,python-3.x,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 1

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

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()

如果
文件中存在
检查
中的所有项目,则变量
结果
的值将为True

checks = ["line1", "line2"]
file = ["line3", "line4", line5", "line1", "line2"]
result = (all([True for line in checks if line in file]))

将要搜索的行定义为列表,并在文件中运行上述代码。

什么叫“匹配多行”?我想为2行或更多行创建一个匹配。假设我有一行写着“这是line1”,第二行写着“这是line2”。我希望脚本在文件中搜索这两行并返回“通过”或“失败”。