0% found this document useful (0 votes)
3 views

Assignment8

The document outlines an assignment to create a Python program for tracking personal or business expenses using file operations. The program allows users to log, view, modify, delete, and search for expenses, with functionalities to calculate total expenses and analyze spending over time. It includes a menu-driven interface and demonstrates various operations through sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment8

The document outlines an assignment to create a Python program for tracking personal or business expenses using file operations. The program allows users to log, view, modify, delete, and search for expenses, with functionalities to calculate total expenses and analyze spending over time. It includes a menu-driven interface and demonstrates various operations through sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment-8

Program using Files


Name: K.S.Vijayaraaghavan Register Number:3122247001066
Evaluation:

Objective:
Solve python problems using files.
Question:
1. Create a Python program using Files that allows users to log and track their personal or business
expenses. The program should read and write expense data to a file, enabling users to record new
transactions, view total expenses, and analyse spending over time.

Algorithm:
Code:
import os

# File to store expenses


expense_file = 'expenses.txt'

# Function to display the menu


def display_menu():
print("\nExpense Tracker Menu:")
print("1. Log a new expense")
print("2. View all expenses")
print("3. View total expenses")
print("4. Search expenses by date or description")
print("5. Delete an expense")
print("6. Modify an expense")
print("7. Exit")

# Function to log a new expense


def log_expense():
description = input("Enter expense description: ")
amount = float(input("Enter amount spent: "))
date = input("Enter date of expense (DD.MM.YY): ")

with open(expense_file, 'a') as file:


file.write(f"{description},{amount},{date}\n")

print("Expense logged successfully.")

# Function to view all expenses


def view_expenses():
if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return

with open(expense_file, 'r') as file:


expenses = file.readlines()

if expenses:
print("\nLogged Expenses:")
for expense in expenses:
description, amount, date = expense.strip().split(',')
print(f"Description: {description}, Amount: {amount}, Date: {date}")
else:
print("No expenses recorded.")

# Function to calculate total expenses


def calculate_total_expenses():
if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return

total = 0.0
with open(expense_file, 'r') as file:
for line in file:
_, amount, _ = line.strip().split(',')
total += float(amount)

print(f"Total Expenses: {total}")

# Function to search expenses by description or date


def search_expenses():
search_term = input("Enter search term (description or date): ")

if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return

with open(expense_file, 'r') as file:


expenses = file.readlines()

found = False
print("\nMatching Expenses:")
for expense in expenses:
description, amount, date = expense.strip().split(',')
if search_term in description or search_term in date:
print(f"Description: {description}, Amount: {amount}, Date: {date}")
found = True

if not found:
print("No matching expenses found.")

# Function to delete an expense


def delete_expense():
search_term = input("Enter description or date of the expense to delete: ")

if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return

with open(expense_file, 'r') as file:


expenses = file.readlines()

with open(expense_file, 'w') as file:


found = False
for expense in expenses:
if search_term not in expense:
file.write(expense)
else:
found = True

if found:
print("Expense deleted successfully.")
else:
print("Expense not found.")

# Function to modify an expense


def modify_expense():
search_term = input("Enter description or date of the expense to modify: ")

if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return

with open(expense_file, 'r') as file:


expenses = file.readlines()

with open(expense_file, 'w') as file:


found = False
for expense in expenses:
if search_term in expense:
description, _, date = expense.strip().split(',')
new_description = input(f"Enter new description (current: {description}):
")
new_amount = float(input(f"Enter new amount (current:
{expense.split(',')[1]}): "))
new_date = input(f"Enter new date (current: {date}): ")
file.write(f"{new_description},{new_amount},{new_date}\n")
found = True
else:
file.write(expense)

if found:
print("Expense modified successfully.")
else:
print("Expense not found.")

# Main program function


def main():
while True:
display_menu()
choice = input("Enter your choice (1-7): ")

if choice == '1':
log_expense()
elif choice == '2':
view_expenses()
elif choice == '3':
calculate_total_expenses()
elif choice == '4':
search_expenses()
elif choice == '5':
delete_expense()
elif choice == '6':
modify_expense()
elif choice == '7':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")

main()
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-8/a8-1.py

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 2
No expenses recorded yet.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 1
Enter expense description: Transport
Enter amount spent: 100.0
Enter date of expense (DD.MM.YY): 01.04.25
Expense logged successfully.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 1
Enter expense description: Merch
Enter amount spent: 200.0
Enter date of expense (DD.MM.YY): 17.04.25
Expense logged successfully.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 2

Logged Expenses:
Description: Transport, Amount: 100.0, Date: 01.04.25
Description: Merch, Amount: 200.0, Date: 17.04.25

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 3
Total Expenses: 300.0

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 4
Enter search term (description or date): merch

Matching Expenses:
No matching expenses found.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 4
Enter search term (description or date): Merch

Matching Expenses:
Description: Merch, Amount: 200.0, Date: 17.04.25

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 5
Enter description or date of the expense to delete: Merch
Expense deleted successfully.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7):
Invalid choice. Please try again.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 6
Enter description or date of the expense to modify: Transport
Enter new description (current: Transport): Logistics
Enter new amount (current: 100.0): 360.0
Enter new date (current: 01.04.25): 01.04.25
Expense modified successfully.

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 2

Logged Expenses:
Description: Logistics, Amount: 360.0, Date: 01.04.25

Expense Tracker Menu:


1. Log a new expense
2. View all expenses
3. View total expenses
4. Search expenses by date or description
5. Delete an expense
6. Modify an expense
7. Exit
Enter your choice (1-7): 7
Exiting the program.

Learning Outcomes:

You might also like