Skip to content
TechCareer Lab

Command-Line Expense Tracker in Python

Build a command-line tool that records expenses in a CSV file and prints monthly summaries, using only Python's standard library.

Beginner

Overview

You will build expenses.py, a command-line tool for recording daily expenses and summarising them by month and category. It uses only Python's standard library, so there is nothing extra to install.

Track the project with Git from the start and commit after each working feature.

Features to build

The finished tool should support commands like these:

python expenses.py add 250 food "Lunch with team"
python expenses.py list
python expenses.py summary 2026-12
  • add: record an amount, a category and an optional note, with today's date.
  • list: show all recorded expenses in a readable table.
  • summary: show the total for a month, broken down by category.

Suggested structure

import argparse
import csv
from datetime import date
from pathlib import Path
 
DATA_FILE = Path("expenses.csv")
FIELDS = ["date", "amount", "category", "note"]
 
 
def add_expense(amount: float, category: str, note: str) -> None:
    is_new = not DATA_FILE.exists()
    with DATA_FILE.open("a", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        if is_new:
            writer.writeheader()
        writer.writerow(
            {"date": date.today().isoformat(), "amount": amount,
             "category": category, "note": note}
        )
 
 
def main() -> None:
    parser = argparse.ArgumentParser(description="Track your expenses")
    sub = parser.add_subparsers(dest="command", required=True)
 
    add = sub.add_parser("add", help="Record an expense")
    add.add_argument("amount", type=float)
    add.add_argument("category")
    add.add_argument("note", nargs="?", default="")
 
    args = parser.parse_args()
    if args.command == "add":
        add_expense(args.amount, args.category, args.note)
 
 
if __name__ == "__main__":
    main()

Extend this starting point with the list and summary commands.

Tasks

  1. Get the add command working and commit.
  2. Implement list, formatting amounts to two decimal places.
  3. Implement summary for a given month (YYYY-MM).
  4. Reject negative amounts with a clear error message.
  5. Write a short README.md explaining how to use the tool.

Stretch goals

  • Add a --category filter to list.
  • Export a monthly summary to a new CSV file.
  • Add unit tests for your summary function using unittest.
  • Demo

    Git Basics: Create Your First Repository

    Set up Git, create a repository, make commits, and work with branches. The core commands every developer uses daily, explained step by step.

    3 min read