CodingNic

Files, Errors, and Automation

Final Capstone with Saved Data

Files, Errors, and Automation 60 min read

Final Capstone with Saved Data

Final Capstone with Saved Data

Great job finishing this module.

Now it is time to build one real project that uses everything from this module together.

You will build a program that:

  • Saves data
  • Loads data
  • Handles errors
  • Uses folders
  • Creates backups
  • Automates repeated tasks

Project: Personal Expense Tracker

This program will help users track spending and save records in a JSON file.

Skills You Will Use

  • reading files
  • writing files
  • append logic
  • JSON
  • error handling
  • os
  • shutil
  • loops
  • functions
  • conditions

Step 1: Create Project Folder

Create a folder named:

text
expense_data

Inside it, use this file:

text
expense_data/expenses.json

Step 2: Starter Data

If the file does not exist, create it with:

python
[
    {"item": "Lunch", "amount": 12},
    {"item": "Bus", "amount": 5},
    {"item": "Book", "amount": 20}
]

Step 3: Main Menu

Show this menu in a loop:

text
1. Show Expenses
2. Add Expense
3. Highest Expense
4. Total Spent
5. Backup Data
6. Exit

Ask the user to choose an option.


Step 4: Create Functions

load_expenses()

Read expenses.json and return the list.

Use try/except to handle missing file errors.

If the file is missing, return an empty list.

save_expenses(data)

Save the expense list into JSON.

Use indent=4.

show_expenses()

Print all items and amounts.

Example:

text
Lunch 12
Bus 5
Book 20

add_expense()

Ask for:

  • item name
  • amount

Add a new dictionary:

python
{"item": "Coffee", "amount": 4}

Then save the updated list.

highest_expense()

Find the largest amount.

Print:

text
Highest: Book 20

total_spent()

Add all amounts.

Print:

text
Total: 37

backup_data()

Create a folder named:

text
backup

Copy expenses.json into it using shutil.copy().

Then print:

text
Backup created

Step 5: Exit

If user chooses 6, print:

text
Goodbye

Then stop the loop.


Full Example Run

text
1. Show Expenses
2. Add Expense
3. Highest Expense
4. Total Spent
5. Backup Data
6. Exit

Choose: 1
Lunch 12
Bus 5
Book 20

Choose: 4
Total: 37

Choose: 5
Backup created

Choose: 6
Goodbye

Extra Challenges

After finishing, try adding:

  • Delete expense
  • Search by item
  • Sort by highest amount
  • Monthly budget warning
  • Export CSV report
  • Category tracking

Real World Use Case

This project uses the same ideas found in budgeting apps, finance dashboards, and business expense tools.


Assignment

Build the full project. Then add one extra feature.


Summary

You built a real Python program that saves expenses, loads records, handles errors, and creates backups automatically.