-
Notifications
You must be signed in to change notification settings - Fork 5
/
CS_Source Code.py
245 lines (220 loc) · 6.15 KB
/
CS_Source Code.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""to maintain student details
roll number
name
percentage
"""
"""
modules imported
"""
import pickle
import os
import time
"""
class used
"""
class student(object):
def __init__(self):
self.roll = 0
self.name = ""
self.per = 0
def add_rec(self):
self.roll = int(input("Enter roll number: "))
self.name = input("Enter name: ")
self.name = self.name.upper()
self.per = float(input("Enter percentage: "))
def disp_rec(self):
print("roll number: ", self.roll)
print("name: ", self.name)
print("percentage: ", self.per)
def display_rec(self):
print("%-10s" % self.roll, "%-20s" % self.name, "%-10s" % self.per)
# print("in display_rec")
def modify_rec(self):
self.roll = int(input("Enter new roll number: "))
self.name = input("Enter new name: ")
self.name = self.name.upper()
self.per = float(input("Enter new percentage: "))
def write_record():
try:
rec = student()
file = open("stud.dat", "ab")
rec.add_rec()
pickle.dump(rec, file)
file.close()
print("Record added in file!")
input("Press any key to continue....")
except:
pass
def display_all():
print(40 * "=")
print("\n\t Student Records\n")
print(40 * "=")
try:
file = open("stud.dat", "rb")
while True:
rec = pickle.load(file)
rec.display_rec()
except EOFError:
file.close()
print(40 * "=")
input("Press any key to continue....")
except IOError:
print("File could not be opened!!")
def search_roll():
try:
z = 0
print(40 * "=")
print("Record searching by roll number...")
print(40 * "=")
n = int(input("Enter roll number to search: "))
file = open("stud.dat", "rb")
while True:
rec = pickle.load(file)
# print(rec.roll)
if rec.roll == n:
z = 1
print("\nRecord found and details are:\n")
rec.disp_rec()
break
except EOFError:
file.close()
if z == 0:
print("Record is not present!!")
except IOError:
print("File could not be opened!!")
input("Press any key to continue....")
def search_name():
try:
z = 0
print(40 * "=")
print("Record searching by name...")
print(40 * "=")
n = input("Enter name to search: ")
file = open("stud.dat", "rb")
while True:
rec = pickle.load(file)
# print(rec.roll)
if rec.name == n.upper():
z = 1
print("\nRecord found and details are:\n")
rec.disp_rec()
break
except EOFError:
file.close()
if z == 0:
print("Record is not present!!")
except IOError:
print("File could not be opened!!")
input("Press any key to continue....")
def modify_roll():
z = 0
try:
n = int(input("Enter roll number to modify: "))
file = open("stud.dat", "rb")
temp = open("temp.dat", "wb")
while True:
rec = pickle.load(file)
if rec.roll == n:
z = 1
print("\nRecord found and details are:\n")
rec.disp_rec()
print("\nEnter new data...")
rec.modify_rec()
pickle.dump(rec, temp)
print("Record updated!")
else:
pickle.dump(rec, temp)
except EOFError:
file.close()
temp.close()
if z == 0:
print("Record not found!!")
except IOError:
print("File could not be opened!!")
os.remove("stud.dat")
os.rename("temp.dat", "stud.dat")
input("Press any key to continue....")
def delete_roll():
z = 0
try:
n = int(input("Enter roll number to delete: "))
file = open("stud.dat", "rb")
temp = open("temp.dat", "wb")
while True:
rec = pickle.load(file)
if rec.roll == n:
z = 1
print("\nRecord to delete found and details are:\n")
rec.disp_rec()
# pickle.dump(rec,temp)
print("Record updated!")
else:
pickle.dump(rec, temp)
except EOFError:
file.close()
temp.close()
if z == 0:
print("Record not found!!")
except IOError:
print("File could not be opened!!")
os.remove("stud.dat")
os.rename("temp.dat", "stud.dat")
input("Press any key to continue....")
if not os.path.isfile("stud.dat"):
print("'stud.dat' file doesn't exist.")
print("1 -> Generate a new empty file")
print("2 -> Generate a file with fake data")
print(40 * "=")
choice = input("Enter your choice: ")
print(40 * "=")
if choice == "1":
try:
file = open("stud.dat", "wb")
file.close()
print("New empty file 'stud.dat' created.")
except Exception as e:
print(f"Error: {e}")
elif choice == "2":
print("Generating fake data...")
os.system("python create_dat.py")
print("Fake data generated.")
else:
print("Invalid choice!")
time.sleep(1)
while True:
os.system("cls" if os.name == "nt" else "clear")
print(40 * "=")
print(
""" Main Menu
---------
1. Add record
2. Display all records
3. Search by roll number
4. Search by name
5. Modify by roll number
6. Delete by roll number
7. Exit
"""
)
print(40 * "=")
ch = int(input("Enter your choice: "))
print(40 * "=")
if ch == 1:
write_record()
elif ch == 2:
display_all()
elif ch == 3:
search_roll()
elif ch == 4:
search_name()
elif ch == 5:
modify_roll()
elif ch == 6:
delete_roll()
elif ch == 7:
print("\n\t !!!End!!!\n")
time.sleep(2)
break
else:
print("Invalid choice!!")
time.sleep(1)