forked from qaidjoharj53/Student-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS_Source Code.py
197 lines (197 loc) · 5.36 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
"""to maintain student details
roll number
name
percentage
"""
"""
modules imported
"""
import pickle
import os
import time
"""
class used
"""
class student(object):
def __int__(s):
s.roll=0
s.name=""
s.per=0
def add_rec(s):
s.roll=int(input("Enter roll number: "))
s.name=input("Enter name: ")
s.name=s.name.upper()
s.per=float(input("Enter percentage: "))
def disp_rec(s):
print("roll number: ",s.roll)
print("name: ",s.name)
print("percentage: ",s.per)
def display_rec(s):
print("%-10s"%s.roll,"%-20s"%s.name,"%-10s"%s.per)
#print("in display_rec")
def modify_rec(s):
s.roll=int(input("Enter new roll number: "))
s.name=input("Enter new name: ")
s.name=s.name.upper()
s.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....")
while True:
os.system("cls")
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)