A command-line expense tracker in Python. Record expenses, see the total and per-category spending, filter by category, and keep everything in a local CSV file between runs.
It uses only the Python standard library and has 11 unittest tests.
- Add expenses: amount, category and description, with today's date filled in automatically. You can add several in one go.
- Input validation: amounts must be numbers greater than zero, and text fields can't be empty.
- Spending report: the total of all expenses, plus a total for each category.
- Filter by category: matching ignores capitalisation and surrounding spaces, and the filtered view shows its own total.
- CSV persistence: expenses are saved to
expenses.csvand loaded again when the app starts. The file is created automatically and is git-ignored. - Tests: totals (including empty, negative and large values), filtering, and saving/loading the CSV file.
Python 3 (standard library only): csv, pathlib, datetime, unittest.
1. Add an expense
2. View all expenses
3. View the spending report
4. Filter expenses by category
5. Exit
please select an option (1-5): 3
========= EXPENSE REPORT ==========
Total expenses: 195.50
Spending by category:
Food: 150.00
Transport: 45.50
===================================
Requires Python 3.10 or newer. Nothing to install.
git clone https://github.com/alymatar/expense-analytics-cli.git
cd expense-analytics-cli
python main.py
python -m unittest test_expenses.py
expense-analytics-cli/
├── main.py # menu, input validation, calculations, CSV load/save
├── test_expenses.py # unittest suite (11 tests)
├── README.md
└── .gitignore # ignores expenses.csv, caches and virtualenvs
- Structuring a program as small, testable functions
- Validating user input with
try/except - Reading and writing CSV files with
csv.DictReader/DictWriterandpathlib - Writing unit tests, including tests that use temporary files
- Edit and delete expenses, and filter by date range
- Move storage from CSV to SQLite or PostgreSQL
- Expose the same logic as a REST API with FastAPI