Student Management CSV System

Заказчик: AI | Опубликовано: 02.03.2026

import csv from security import hash_password STUDENT_FILE = "students.csv" CREDENTIALS_FILE = "credentials.csv" def validate_student_header(fieldnames): return fieldnames == ["name", "roll", "class", "marks"] def read_students(): students = [] try: with open(STUDENT_FILE, newline="") as file: reader = csv.DictReader(file) if not validate_student_header(reader.fieldnames): print("Invalid students.csv format.") return [] for row in reader: students.append(row) except FileNotFoundError: with open(STUDENT_FILE, "w", newline="") as file: writer = csv.writer(file) writer.writerow(["name", "roll", "class", "marks"]) return students def write_students(students): with open(STUDENT_FILE, "w", newline="") as file: writer = csv.DictWriter( file, fieldnames=["name", "roll", "class", "marks"] ) writer.writeheader() writer.writerows(students) def add_student(): print("\n--- Add New Student ---") name = input("Name: ").strip() roll = input("Roll Number: ").strip() class_name = input("Class: ").strip() marks = input("Marks: ").strip() if not all([name, roll, class_name, marks]): print("All fields required.") return if not marks.isdigit(): print("Marks must be numeric.") return students = read_students() for student in students: if student["roll"] == roll: print("Roll number already exists.") return students.append({ "name": name, "roll": roll, "class": class_name, "marks": marks }) write_students(students) # Create student login password = input("Set student password: ").strip() hashed = hash_password(password) with open(CREDENTIALS_FILE, "a", newline="") as file: writer = csv.writer(file) writer.writerow([roll, hashed, "Student"]) print("Student added successfully.") def list_students(): students = read_students() if not students: print("No records found.") return print("\n{:<20}{:<15}{:<15}{:<10}".format( "Name", "Roll", "Class", "Marks")) print("-" * 60) for s in students: print("{:<20}{:<15}{:<15}{:<10}".format( s["name"], s["roll"], s["class"], s["marks"] )) def search_student(): roll = input("Enter roll number: ").strip() student = get_student_by_roll(roll) if student: print("\nRecord Found:") print(f"Name : {student['name']}") print(f"Roll : {student['roll']}") print(f"Class : {student['class']}") print(f"Marks : {student['marks']}") else: print("Student not found.") def update_marks(): roll = input("Enter roll number: ").strip() students = read_students() for student in students: if student["roll"] == roll: new_marks = input("New marks: ").strip() if not new_marks.isdigit(): print("Marks must be numeric.") return student["marks"] = new_marks write_students(students) print("Marks updated.") return print("Student not found.") def delete_student(): roll = input("Enter roll number: ").strip() students = read_students() updated = [s for s in students if s["roll"] != roll] if len(updated) == len(students): print("Student not found.") return write_students(updated) # Remove credentials try: with open(CREDENTIALS_FILE, newline="") as file: reader = list(csv.reader(file)) header = reader[0] rows = reader[1:] with open(CREDENTIALS_FILE, "w", newline="") as file: writer = csv.writer(file) writer.writerow(header) for row in rows: if row[0] != roll: writer.writerow(row) except FileNotFoundError: pass print("Student deleted.") def get_student_by_roll(roll): students = read_students() for student in students: if student["roll"] == roll: return student return None