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

时间:2016-06-27 14:38:10

标签: python logfiles

我需要读取文件,然后获取文件的某些行,然后将我需要的行写入另一个文件。我有点卡住,似乎无法找到解决方案。需要从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

1 个答案:

答案 0 :(得分:1)

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

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