35 — Diary: notes saved by date
A diary is one file per day. Python's datetime package tells you today's
date, and the file helper from guide 13 saves and reads the
note. Put the two together and every note lands in a file named after its day.
Steps
Get today's date as text.
date.today()from thedatetimepackage (guide 24) knows the real date, andstr()turns it into the text2026-08-11:
run it →from datetime import date today = str(date.today()) show todayThe output is the real current date, so it shows today's date on any day.
Save a note into a file whose name contains the date. The
finf"..."fills{today}into the filename, thenfile_writeandfile_readfrom guide 13 store and load the note:
run it →use file latest from datetime import date today = str(date.today()) file_write(f"diary-{today}.txt", "Had coffee with a friend.") show file_read(f"diary-{today}.txt")A new file appears in the folder for each day the diary is used.
The whole diary is one menu loop, like the terminal menu from guide 22:
addsaves a note,readshows today or a past date, andquitbreaks the loop. Save it asdiary.nme:
run it →# A diary: each day's note goes to its own dated file. # Run: nme r diary use file latest from datetime import date show "diary menu (add, read, quit)" while True: ask action, "choice (add, read, quit): " if action == "add": ask note, "note: " today = str(date.today()) file_write(f"diary-{today}.txt", note) show "saved to " + f"diary-{today}.txt" elif action == "read": ask when, "which day (today, date): " if when == "today": today = str(date.today()) show file_read(f"diary-{today}.txt") else: ask day, "date (YYYY-MM-DD): " show file_read("diary-" + day + ".txt") else: show "bye" breakRun it and feed the menu three answers — add a note, read today, quit:
printf 'add\nHad coffee with a friend.\nread\ntoday\nquit\n' | nme r diarydiary menu (add, read, quit) choice (add, read, quit): note: saved to diary-2026-08-11.txt choice (add, read, quit): which day (today, date): Had coffee with a friend. choice (add, read, quit): byeThe filename shows the real date; yours prints today's date instead.
A past date is read the same way in reverse:
askcollects the date andfile_readopens that exact file. That is theread datebranch:
run it →use file latest ask when, "which day (today, date): " if when == "today": today = str(date.today()) show file_read(f"diary-{today}.txt") else: ask day, "date (YYYY-MM-DD): " show file_read("diary-" + day + ".txt")The branch checks
when, and only opensdiary-<date>.txtwhen the day is not today.
Try it yourself
Add a list choice that shows every diary file in the folder. from pathlib import Path and a for loop over Path(".").glob("diary-*.txt") lists the
dated files:
from pathlib import Path
for p in sorted(Path(".").glob("diary-*.txt")):
show p.name
run it →Add list to the menu prompt and a new elif action == "list": branch that
runs this loop.
What you learned
from datetime import dateandstr(date.today())give today's date as text.f"diary-{today}.txt"builds a filename from the date.file_writesaves a note to that file andfile_readreads it back.- A
while True:menu turns one note per day into a growing diary.