Binary file update using Python program
#Create a binary file with roll number, name and marks. Input a roll number and update the marks.
import pickle
def write():
stud=[]
f=open("test.dat","wb")
ans='y'
while ans.lower()=='y':
rollno=int(input("Enter Roll No:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
stud.append([rollno,name,marks])
ans=input("Add more records?(y)")
pickle.dump(stud,f)
f.close()
def update():
found=False
f = open("test.dat","rb+")
n=int(input("enter roll number:"))
try:
while True:
rpos=f.tell()
stu=pickle.load(f)
if stu[0][0]==n:
stu[0][2]=input("enter new marks for the above roll no:")
f.seek(rpos)
pickle.dump(stu,f)
found=True
except EOFError:
if found==False:
print("sorry")
else:
print("successfully updated.")
f.close()
def read():
f = open("test.dat","rb")
try:
while True:
print(pickle.load(f))
except EOFError:
f.close()
write()
update()
read()
Comments
Post a Comment