将某些消息从一个日志传输到另一个

Transferring certain messages from a log to another

我需要读取一个文件,然后取出文件的某些行,然后将我需要的行写入另一个文件。我有点卡住了,似乎找不到解决办法。需要取自 bi_server1-diagnostic.log 的行需要在其中包含单词 biserviceadministration。然后它们就可以写入到 SAC 报告的日志文件中。这是我目前所拥有的;不过我觉得这是错误的:

with open('bi_server1-diagnostic.log', 'r') as infile:
    for line in infile:
        if 'biserviceadministration' in line:
            with open ('SAC Report.log', 'w') as outfile:
                outfile.write

你们真的很亲密。你的问题是什么?我会使用文件的完整路径,但也可以这样做。

with open('bi_server1-diagnostic.log', 'r') as infile:
    with open ('SAC Report.log', 'w') as outfile:
        for line in infile:
            if 'biserviceadministration' in line:
                outfile.write(line)
                #print(line, file=outfile) alternatively
            else:
                continue