-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler.py
More file actions
98 lines (68 loc) · 2.09 KB
/
Copy pathfilehandler.py
File metadata and controls
98 lines (68 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# filehandler.py
import csv
import os
from result import Result
FILE_NAME = "students.csv"
# -----------------------------
# Create CSV File with Header
# -----------------------------
def create_file():
if not os.path.exists(FILE_NAME):
with open(FILE_NAME, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow([
"Roll Number",
"Student Name",
"English",
"Maths",
"Science",
"Total",
"Percentage",
"Grade"
])
# -----------------------------
# Save Student
# -----------------------------
def save_student(student):
create_file()
total = Result.total(student)
percentage = Result.percentage(student)
grade = Result.grade(student)
with open(FILE_NAME, "a", newline="") as file:
writer = csv.writer(file)
writer.writerow([
student.roll_no,
student.name,
student.english,
student.maths,
student.science,
total,
f"{percentage:.2f}",
grade
])
print("\n✅ Student record saved successfully.")
# -----------------------------
# View All Students
# -----------------------------
def view_students():
create_file()
with open(FILE_NAME, "r", newline="") as file:
reader = csv.reader(file)
next(reader)
students = list(reader)
if len(students) == 0:
print("\nNo Student Records Found.")
return
print("\n" + "=" * 80)
print(" ALL STUDENT RECORDS")
print("=" * 80)
for row in students:
print(f"Roll Number : {row[0]}")
print(f"Name : {row[1]}")
print(f"English : {row[2]}")
print(f"Maths : {row[3]}")
print(f"Science : {row[4]}")
print(f"Total : {row[5]}")
print(f"Percentage : {row[6]}%")
print(f"Grade : {row[7]}")
print("-" * 80)