← Back to course
Code Crew G6+ / Week 08 / Friday
5/6
Week 08 Β· Files

Friday

A log viewer
// Saving work that lasts
⏱ about 30 min

Friday: A log viewer

The log has grown to dozens of lines. Now the flight director has a question: when did we last test the docking game? Rocket scrolls and scrolls.

"There has to be a search," he says. "I am a programmer. I should not be scrolling."

Raven opens her notebook to a page with a list of line numbers. "What do you notice?" she asks. "We need two things: every line with a number, and only the lines that match a word."

Nova shines a narrow beam across the log, lighting up one line at a time. "Would you like a hint?" she asks. "The in operator works on strings too. It tells you whether one string appears inside another."

Rocket reads the whole log into a list first, so he can use it twice.

in works on strings

"test" in "Engine test passed" is True, because the letters test appear inside the longer string. Comparing lowercase versions makes the search ignore capital letters.

# log_viewer.py
LOG_FILE = "mission_log.txt"

lines = []
with open(LOG_FILE) as f:
    for line in f:
        lines.append(line.strip())

print(f"{len(lines)} lines in the log")
for number, line in enumerate(lines, start=1):
    print(f"{number}. {line}")

word = input("Search for: ").strip().lower()
found = 0
for line in lines:
    if word in line.lower():
        print("  found:", line)
        found = found + 1
print(found, "matching lines")

With this four-line log in the folder, and a search for TEST:

4 lines in the log
1. 2026-04-10 Started the engine test
2. 2026-04-10 Engine test passed
3. 2026-04-11 Fixed the countdown clock
4. 2026-04-11 Tested the docking game
Search for: TEST
  found: 2026-04-10 Started the engine test
  found: 2026-04-10 Engine test passed
  found: 2026-04-11 Tested the docking game
3 matching lines
Why does line 4 match test? It contains the word ____
A search for clock finds how many lines?
A search for 2026-04-10 finds how many lines?
READ THE VIEWER
  • Read the question.
  • Tap your answer.
Why does the program store the lines in a list first?
At your computer
1. Make sure mission_log.txt from yesterday is in your mission folder.
2. Type log_viewer.py, save it and run it. Search for test.
3. Run it again and search for a word that is not in the log. What does it say?
4. Challenge: when found is 0, print a friendly message that suggests trying another word.

Practice: save the search results

A program can read one file and write another. This one copies every line about tests into a new file called tests.txt.

# export_tests.py
kept = 0
with open("mission_log.txt") as source:
    with open("tests.txt", "w") as target:
        for line in source:
            if "test" in line.lower():
                target.write(line)
                kept = kept + 1
print("Saved", kept, "lines to tests.txt")
Saved 3 lines to tests.txt
READ EXPORT_TESTS.PY
  • Read the question.
  • Tap your answer.
Why does target.write(line) not need \n added?
Which log line is not copied?
Why is mission_log.txt opened with no mode?
Change "test" to "clock". How many lines are saved?
Change "test" to "2026". How many lines are saved?
What does "Engine Test".lower() give?
StatementTrue or false?
A program can have two files open at once.?
"test" in "Tested" is True.?
"test" in "tested" is True.?
Mode "w" on tests.txt keeps last run's results.?
WHY THIS EXERCISECapital T is not small t, so lowering the line first is what makes the search ignore case.

You can now save, grow, number and search a log. That is most of Mission Log already. Tomorrow is a Crew Quest.

← Thursday