pythonlogfiles

Transferring certain messages from a log to another


I need to read a file, then take certain lines of the file, then write the lines I need to another file. I'm kinda stuck and can't seem to find a solution. The lines that need to be taken from bi_server1-diagnostic.log need to have the word biserviceadministration in them. Then they can be written onto the log file SAC report. This is what I have so far; I get the feeling it is wrong though:

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

Solution

  • You were really close. What was your problem? I would use the full paths for the files but it can also be done like that.

    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