← Back to course
Code Crew G6+ / Week 08 / Thursday
4/6
Week 08 Β· Files

Thursday

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

Thursday: A dated log

The flight director wants every important event at Mission Control written down with its date. Not on sticky notes, not on the whiteboard, but in one log file that keeps growing.

"I could write the date by hand each time," Rocket says, "but I would get it wrong at least once a week."

Raven thinks back over the last two weeks. "What do you notice?" she asks. "We know how to get today's date. We know how to append to a file. We know how to wrap a job in a function."

Nova arranges three glowing pieces into one shape. "I found a pattern," she says. "Three things you already know, combined into one new tool."

Rocket writes a function called log_event.

A logging function

LOG_FILE is written in capitals because it never changes while the program runs. Programmers use capital names for settings like this.

The function log_event() adds one line: today's date, a space and the text.

The function count_lines() reads the file with readlines(), which gives a list of lines, and counts them.

# log_event.py
from datetime import date

LOG_FILE = "mission_log.txt"

def log_event(text):
    """Add one dated line to the log file."""
    stamp = date.today().isoformat()
    with open(LOG_FILE, "a") as f:
        f.write(f"{stamp} {text}\n")

def count_lines(filename):
    """How many lines a text file has."""
    with open(filename) as f:
        return len(f.readlines())

log_event("Started the engine test")
log_event("Engine test passed")
print("Log has", count_lines(LOG_FILE), "lines")

After two runs, mission_log.txt holds four lines. Your dates will be the days you run it:

2026-04-12 Started the engine test
2026-04-12 Engine test passed
2026-04-12 Started the engine test
2026-04-12 Engine test passed
The first run shows: Log has ____ lines
After a third run, how many lines does the log have?
WHY A FUNCTION?
  • Read the question.
  • Tap your answer.
Why is log_event a function instead of three lines copied everywhere?
Why does isoformat() suit a log?
At your computer
1. Type log_event.py, save it in your mission folder and run it. Check for 2 lines.
2. Run it again. Check for 4 lines.
3. Open mission_log.txt from IDLE with File, then Open, and read it.
4. Change the two log_event calls to describe something you did today. Run it once more.

Practice: split a log line

Each log line is a date, a space, then the text. The split(" ", 1) method cuts at the first space only, giving a list of two pieces.

That lets a program count entries per day with a dictionary.

# per_day.py
counts = {}
with open("mission_log.txt") as f:
    for line in f:
        day, text = line.strip().split(" ", 1)
        counts[day] = counts.get(day, 0) + 1
for day, number in counts.items():
    print(f"{day}: {number} entries")
2026-04-10: 2 entries
2026-04-11: 2 entries
READ PER_DAY.PY
  • Read the question.
  • Tap your answer.
What does "2026-04-10 Engine test passed".split(" ", 1) give?
What does day, text = ... do with the two pieces?
Why is a dictionary used for counts?
What does "a b c".split(" ") give? How many pieces?
What does "a b c".split(" ", 1) give? How many pieces?
You add a line for 2026-04-12. What count does that day show?
StatementTrue or false?
split(",") cuts a string at every comma.?
split always gives back exactly two pieces.?
readlines() gives a list of the lines in a file.?
A capital name like LOG_FILE tells readers it is a setting.?
WHY THIS EXERCISEsplit makes one more piece than the number of cuts, so the count depends on the string.
Try it
1. Make sure mission_log.txt is in your mission folder.
2. Type per_day.py, save it and run it.
3. Run log_event.py once more, then run per_day.py again.
4. Check that today now has two more entries.

You built a dated, growing log. Tomorrow you will build a viewer that numbers and searches it.

← Wednesday