Here is a Python project for Class 12 CBSE students. The project is a “Student Management System” that uses a CSV file as the backend to store and retrieve student records. It includes full functionality and can be submitted as part of a school project.
Python Code
import csv
import os
def initialize_csv(file_name):
"""Initialize the CSV file with headers if it does not exist."""
if not os.path.exists(file_name):
with open(file_name, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Roll Number", "Name", "Class", "Marks"])
def add_student(file_name):
"""Add a new student record to the CSV file."""
roll_number = input("Enter Roll Number: ")
name = input("Enter Name: ")
student_class = input("Enter Class: ")
marks = input("Enter Marks: ")
with open(file_name, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([roll_number, name, student_class, marks])
print("Student record added successfully!\n")
def view_students(file_name):
"""Display all student records from the CSV file."""
try:
with open(file_name, mode='r') as file:
reader = csv.reader(file)
print("\nStudent Records:")
print("----------------------------------------")
for row in reader:
print("\t".join(row))
print("----------------------------------------\n")
except FileNotFoundError:
print("No records found. Please add some students first.\n")
def search_student(file_name):
"""Search for a student record by roll number."""
roll_number = input("Enter Roll Number to search: ")
found = False
try:
with open(file_name, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == roll_number:
print("\nStudent Record Found:")
print("Roll Number: ", row[0])
print("Name: ", row[1])
print("Class: ", row[2])
print("Marks: ", row[3])
found = True
break
if not found:
print("\nStudent record not found!\n")
except FileNotFoundError:
print("No records found. Please add some students first.\n")
def delete_student(file_name):
"""Delete a student record by roll number."""
roll_number = input("Enter Roll Number to delete: ")
rows = []
found = False
try:
with open(file_name, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] != roll_number:
rows.append(row)
else:
found = True
if found:
with open(file_name, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("\nStudent record deleted successfully!\n")
else:
print("\nStudent record not found!\n")
except FileNotFoundError:
print("No records found. Please add some students first.\n")
def update_student(file_name):
"""Update a student's record by roll number."""
roll_number = input("Enter Roll Number to update: ")
rows = []
found = False
try:
with open(file_name, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == roll_number:
print("\nCurrent Details:")
print("Roll Number: ", row[0])
print("Name: ", row[1])
print("Class: ", row[2])
print("Marks: ", row[3])
row[1] = input("Enter new Name: ")
row[2] = input("Enter new Class: ")
row[3] = input("Enter new Marks: ")
found = True
rows.append(row)
if found:
with open(file_name, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("\nStudent record updated successfully!\n")
else:
print("\nStudent record not found!\n")
except FileNotFoundError:
print("No records found. Please add some students first.\n")
def main():
"""Main function to drive the program."""
file_name = "students.csv"
initialize_csv(file_name)
while True:
print("Student Management System")
print("1. Add Student")
print("2. View Students")
print("3. Search Student")
print("4. Delete Student")
print("5. Update Student")
print("6. Exit")
choice = input("Enter your choice (1-6): ")
if choice == '1':
add_student(file_name)
elif choice == '2':
view_students(file_name)
elif choice == '3':
search_student(file_name)
elif choice == '4':
delete_student(file_name)
elif choice == '5':
update_student(file_name)
elif choice == '6':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please try again.\n")
if __name__ == "__main__":
main()
Leave a Reply