0% found this document useful (0 votes)
4 views3 pages

Menu Driven CSV File Program

The document outlines a menu-driven program for managing student records stored in a CSV file using Python. It includes functions to add, display, search, update, and delete records based on student roll numbers. The program utilizes the csv module and handles file operations with error checking for file existence.

Uploaded by

teepika
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)
4 views3 pages

Menu Driven CSV File Program

The document outlines a menu-driven program for managing student records stored in a CSV file using Python. It includes functions to add, display, search, update, and delete records based on student roll numbers. The program utilizes the csv module and handles file operations with error checking for file existence.

Uploaded by

teepika
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

Made with Xodo PDF Reader and Editor

Menu Driven Program - CSV File Using Dictionary

import csv
import os

filename = "[Link]"

# ---------- Add Record ----------


def add_record():
file_exists = [Link](filename)
with open(filename, "a", newline="") as file:
fieldnames = ["RollNo", "Name", "Marks"]
writer = [Link](file, fieldnames=fieldnames)

if not file_exists:
[Link]()

student = {}
student["RollNo"] = input("Enter Roll Number: ")
student["Name"] = input("Enter Name: ")
student["Marks"] = input("Enter Marks: ")

[Link](student)
print("Record Added Successfully.")

# ---------- Display Records ----------


def display_record():
try:
with open(filename, "r") as file:
reader = [Link](file)
for row in reader:
print(row)
except FileNotFoundError:
print("File does not exist.")

# ---------- Search Record ----------


def search_record():
roll = input("Enter Roll Number to search: ")
found = False
try:
with open(filename, "r") as file:
reader = [Link](file)
for row in reader:
if row["RollNo"] == roll:
print("Record Found:", row)
found = True
break
if not found:
print("Record Not Found.")
except FileNotFoundError:
print("File does not exist.")

# ---------- Update Record ----------


def update_record():
roll = input("Enter Roll Number to update: ")
Made with Xodo PDF Reader and Editor

rows = []
found = False

try:
with open(filename, "r") as file:
reader = [Link](file)
for row in reader:
if row["RollNo"] == roll:
row["Name"] = input("Enter New Name: ")
row["Marks"] = input("Enter New Marks: ")
found = True
[Link](row)

with open(filename, "w", newline="") as file:


fieldnames = ["RollNo", "Name", "Marks"]
writer = [Link](file, fieldnames=fieldnames)
[Link]()
[Link](rows)

if found:
print("Record Updated Successfully.")
else:
print("Record Not Found.")
except FileNotFoundError:
print("File does not exist.")

# ---------- Delete Record ----------


def delete_record():
roll = input("Enter Roll Number to delete: ")
rows = []
found = False

try:
with open(filename, "r") as file:
reader = [Link](file)
for row in reader:
if row["RollNo"] == roll:
found = True
else:
[Link](row)

with open(filename, "w", newline="") as file:


fieldnames = ["RollNo", "Name", "Marks"]
writer = [Link](file, fieldnames=fieldnames)
[Link]()
[Link](rows)

if found:
print("Record Deleted Successfully.")
else:
print("Record Not Found.")
except FileNotFoundError:
print("File does not exist.")

# ---------- Menu ----------


while True:
print("\n===== MENU =====")
Made with Xodo PDF Reader and Editor

print("1. Add Record")


print("2. Display Records")
print("3. Search Record")
print("4. Update Record")
print("5. Delete Record")
print("6. Exit")

choice = input("Enter your choice: ")

if choice == "1":
add_record()
elif choice == "2":
display_record()
elif choice == "3":
search_record()
elif choice == "4":
update_record()
elif choice == "5":
delete_record()
elif choice == "6":
print("Program Ended.")
break
else:
print("Invalid Choice.")

You might also like