You are on page 1of 3

Binary file –Searching

import pickle

file=open("student.dat","wb+")

dict1={}

ans="y"

while ans=="y":

name=input("Enter your name")

roll=input("Enter roll no")

dict1["name"],dict1["roll"]=name,roll

pickle.dump(dict1,file)

ans=input("Do you want to add the record(y/n):")

file.close()

a=input("Enter roll no to search:")

file=open("student.dat","rb")

found=True

try:

while True:

st=pickle.load(file)

if st["roll"]==a:

print("Name of the student:",st["name"])

found=False

break

except EOFError:

if found==True:

print("No record found")

file.close()
output:

Binary file-updating:

import pickle

file=open("student.dat","wb+")

dict1={}

ans="y"

while ans=="y":

name=input("Enter your name")

roll=input("Enter roll no")

mark=float(input("Enter your mark:"))

dict1["name"],dict1["roll"],dict1["mark"]=name,roll,mark

pickle.dump(dict1,file)

ans=input("Do you want to add the record(y/n):")

file.close()

a=input("Enter roll no to search:")

file=open("student.dat","rb+")

found=False
try:

while True:

st=pickle.load(file)

if st["roll"]== a:

mark1=float(input("Enter new mark"))

st["mark"]=mark1

pickle.dump(st,file)

found=True

except EOFError:

if found==True:

print("Changed Successfully")

else:

print("Record not found")

You might also like