Last unit, we started from the very basics — understanding what a file is — and gradually learned how to create, write, read, append, and manage file paths. Students now know how to use open() with different modes ("r", "w", "a") and how the with open(...) as f: pattern ensures files are safely closed. They have seen through examples that "w" overwrites the old content while "a" appends to the end, and learned different ways to read file content using read(), readlines(), or a for line in f: loop.
We also emphasized the importance of path management. Students have learned the difference between relative and absolute paths and practiced using os.path.join() or pathlib.Path to make their programs portable across systems. Together, these skills introduced the idea of data persistence, allowing programs to remember information between runs.
Now students try to finish the following practice to review what we learned last unit:
data exists, then append one line HH:MM:SS message to data/notes.txt.Output example:
OK
Answer example:
import os, time
os.makedirs("data", exist_ok=True)
line = input("Message: ").strip()
with open(os.path.join("data","notes.txt"), "a", encoding="utf-8") as f:
f.write(f"{time.strftime('%H:%M:%S')} {line}n")
print("OK")
The next lesson is to move from raw text storage to structured, standardized data formats. Now, we are ready to explore CSV files, one of the most common and practical formats for saving and sharing tabular data.