← Back to Blog

File Handling Mini Project — Daily Notes

What you're building

A daily notes logger. The user writes a note. It gets saved — with a timestamp — to a text file. Every note appends to the same file. Nothing is ever overwritten.

Run it tomorrow. The notes from today are still there.

What you need to know first

  • File handling — open() with "a" mode
  • Reading a file — open() with "r" mode
  • while True with break
  • input()
  • datetime — Python's built-in module for dates and times

The brief

Build a program that:

  • Shows a menu: 1 — Add note, 2 — View all notes, 3 — Quit
  • On add: asks for a note, saves it to notes.txt with a timestamp — appends, never overwrites
  • On view: reads and prints everything in notes.txt
  • On view: handles the case where the file doesn't exist yet
  • On quit: exits cleanly

The timestamp

Python's datetime module gives you the current date and time:

from datetime import datetime
now = datetime.now().strftime("%Y-%m-%d %H:%M")
print(now)

Output → 2024-05-06 14:32

strftime() formats the datetime as a string. "%Y-%m-%d %H:%M" gives you year-month-day hour:minute.

Think before you code

  • What mode do you use to add a note without losing existing ones?
  • How do you format each entry — timestamp and note on the same line or separate?
  • What do you do if notes.txt doesn't exist when the user tries to view?

Your starting point

from datetime import datetime

FILENAME = "notes.txt"

while True:
    print("\n1 — Add note")
    print("2 — View all notes")
    print("3 — Quit")
    choice = input("Your choice: ")

    # your code here

Expected output

1 — Add note
2 — View all notes
3 — Quit
Your choice: 1
Note: Completed recon on grid 447.
Saved.

Your choice: 1
Note: Equipment check done. All clear.
Saved.

Your choice: 2

--- Notes ---
2024-05-06 14:32 | Completed recon on grid 447.
2024-05-06 14:33 | Equipment check done. All clear.

Your choice: 3
Out.

Heads up!

  • Use "a" mode — every note adds to the end, nothing is lost
  • Use FileNotFoundError when viewing — the file might not exist yet
  • Store the filename in a variable — easier to change later
  • strftime() formats datetime as string — the format is yours to choose

The solution

from datetime import datetime

FILENAME = "notes.txt"

while True:
    print("\n1 — Add note")
    print("2 — View all notes")
    print("3 — Quit")
    choice = input("Your choice: ")

    if choice == "1":
        note = input("Note: ")
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
        with open(FILENAME, "a", encoding="utf-8") as f:
            f.write(f"{timestamp} | {note}\n")
        print("Saved.")

    elif choice == "2":
        try:
            with open(FILENAME, "r", encoding="utf-8") as f:
                content = f.read()
            print("\n--- Notes ---")
            print(content)
        except FileNotFoundError:
            print("No notes yet.")

    elif choice == "3":
        print("Out.")
        break

    else:
        print("Invalid choice. Enter 1, 2, or 3.")
[ login to bookmark ] // copied! 18 views · 2 min
// resources
Exercise daily_notes.py
← prev CSV Files: Structured Data in Plain Text next → File Handling Mini Project — Shopping List
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.