Skip to main content
Programming from zero

Extract error lines into a new file

mediumReading and writing files

Problem statement

Filtering a log is a very common job: read one file, keep only some lines, write them to another file.

The pattern is always the same:

  • Open the source file for reading.
  • Open the target file for writing.
  • For each line, test a condition. If it matches, write it to the target.

You never need the whole log in memory. One line at a time is enough.

Common mistake: opening the source file in write mode.

TEXT
Wrong:
source = open("app.log", "w")

Write mode empties the file the moment it opens. Every log line is gone before you read a single one. Only the target file gets write mode.

Your task: app.log has five lines (the code below creates it). Copy only the lines that contain ERROR into a new file errors.log. Then read errors.log back and print its lines.

Expected output

TEXT
ERROR db timeout
ERROR disk full

Approach

Read every line, and write it out only when it contains ERROR:

  • Python: "ERROR" in line is the test. The line still ends with \n, so target.write(line) keeps the original line breaks.
  • Java: line.contains("ERROR") is the test. readLine() removes the line break, so write line + "\n".
  • C++: line.find("ERROR") != std::string::npos is the test. find returns npos when the text is not present. getline also removes the line break.

The setup lines only create the sample app.log.

# Setup: create a small sample log
with open("app.log", "w") as f:
    f.write("INFO server started\nERROR db timeout\nINFO request ok\nERROR disk full\nWARN cpu high\n")

with open("app.log") as source, open("errors.log", "w") as target:
    for line in source:
        if "ERROR" in line:
            target.write(line)

with open("errors.log") as f:
    for line in f:
        print(line.strip())