Python Series track

Project 11 · Practice Projects

Project 11 — Diary App

Write encrypted daily journal entries, search past entries by keyword or date, and export to Markdown.

Practice Projects1 min readFree lesson

Difficulty: Lv ★★★ | Prior Knowledge: Intermediate 4, Week 6

Goal

CLI for writing, viewing, and searching diary entries by date

Requirements

  • Diary is diary/YYYY-MM-DD.txt
  • Commands: write, read <date>, list, search <keyword>
  • Ask whether to overwrite if rewriting on the same date
  • Multi-line input (end with two blank lines)

Challenge Task

  • Keyword search shows matching line + date
  • Statistics (total days written, average length)

Assignment

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution.pyPython
"""Diary — Files by Date"""
from pathlib import Path
from datetime import date

DIARY = Path("diary")
DIARY.mkdir(exist_ok=True)

def write_today():
    today = date.today().isoformat()
    f = DIARY / f"{today}.txt"
    if f.exists():
        if input(f"{today} already exists. Overwrite? (y/n):").lower() != "y":
            return
    print("Enter content (two empty lines to end):")
    lines = []
    blank = 0
    while True:
        line = input()
        if line == "":
            blank += 1
            if blank >= 2: break
            lines.append("")
        else:
            blank = 0
            lines.append(line)
    f.write_text("\n".join(lines), encoding="utf-8")
    print(f"Saved ({f})")

def read(date_str):
    f = DIARY / f"{date_str}.txt"
    if not f.exists():
        print("None"); return
    print(f.read_text(encoding="utf-8"))

def list_all():
    files = sorted(DIARY.glob("*.txt"))
    for f in files:
        size = len(f.read_text(encoding="utf-8"))
        print(f"{f.stem} ({size} characters)")
    print(f"Total {len(files)}")

def search(keyword):
    for f in sorted(DIARY.glob("*.txt")):
        text = f.read_text(encoding="utf-8")
        for i, line in enumerate(text.splitlines(), 1):
            if keyword in line:
                print(f"[{f.stem} L{i}] {line}")

def main():
    while True:
        raw = input("Command (write/read <date>/list/search <q>/quit):").strip()
        if not raw: continue
        if raw == "quit": break
        parts = raw.split(maxsplit=1)
        cmd = parts[0]
        arg = parts[1] if len(parts) > 1 else ""
        if cmd == "write": write_today()
        elif cmd == "read":
            read(arg if arg else date.today().isoformat())
        elif cmd == "list": list_all()
        elif cmd == "search":
            if arg: search(arg)

if __name__ == "__main__":
    main()

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now