'''Practical No: 9: WAP in Python to create a binary file with roll_no, name and
marks of the students and update the marks of specific student.'''
import pickle
S={}
f=open('stud.dat','wb')
c='y'
while c=='y' or c=='Y':
rno=int(input("Enter the roll no. of the student : "))
name=input("Enter the name of the student: ")
marks=int(input("Enter the marks of the student: "))
S['RollNo']=rno
S['Name']=name
S['Marks']=marks
pickle.dump(S,f)
c=input("Do You Want to add more students(y/n): ")
f.close()
f=open('stud.dat','rb+')
rno=int(input("Enter the roll no. of the student to be updated: "))
marks=int(input("Enter the updated marks of the student: "))
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
S=pickle.load(f)
if S["RollNo"] == rno:
f.seek(pos)
S["Marks"]=marks
pickle.dump(S,f)
m=m+1
except EOFError:
f.close()
if m==0:
print("Student not Found")
else:
f=open('stud.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
No comments:
Post a Comment