0% found this document useful (0 votes)
12 views1 page

Ex - No 11 Program

The document is a Python script that allows users to add student records to a binary file using the pickle module. It prompts for student details, saves them, and provides functionality to search and update a student's marks based on their roll number. Finally, it displays the contents of the updated student records file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Ex - No 11 Program

The document is a Python script that allows users to add student records to a binary file using the pickle module. It prompts for student details, saves them, and provides functionality to search and update a student's marks based on their roll number. Finally, it displays the contents of the updated student records file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import pickle

import os
d1={}
f=open("stu.dat",'ab')
n=int(input("Enter no of records which you want to add in the file"))
for i in range(n):
print("Enter details of students",i+1,":")
d1["Roll"]=int(input("Enter Roll no:"))
d1["Name"]=input("Enter Name:")
d1["Marks"]=float(input("Enter Mark:"))
pickle.dump(d1,f)
f.close()
rollno=int(input("Enter roll number to search and update:"))
f1=open("stu.dat",'rb')
f2=open("temp.dat",'wb')
try:
while True:
d1=pickle.load(f1)
if d1["Roll"]==rollno:
d1["Marks"]=float(input("Enter Mark:"))
pickle.dump(d1,f2)
else:
pickle.dump(d1,f2)
except EOFError:
pass
f1.close()
f2.close()
os.remove("stu.dat")
os.rename("temp.dat","stu.dat")
print("\n Displaying File Conntents:\n")
print("--------------Output-------------------")
with open("stu.dat",'rb') as f:
try:
while True:
d1=pickle.load(f)
print(d1)
except EOFError:
pass

You might also like