Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
program for write read and search data in binary file in python
#writing data in Binary file
import pickle
f=open("ram.dat","wb")
a=[]
while True:
roll=int(input("Enter the Student Roll Number"))
name=input("Enter the Student name")
marks=int(input("Enter the Student marks"))
b=[roll,name,marks]
a.append(b)
bottun=int(input("Press =1 For Continue and Press = 2 for Stop Program"))
if bottun==2:
break
pickle.dump(a,f)
f.close()
#Reading Data in Binary File
import pickle
f=open("ram.dat","rb")
data=pickle.load(f)
for i in data:
print(i)
f.close()
#Searching Data in Binary File
import pickle
f=open("ram.dat","rb")
flag=0
roll=int(input("Enter the Student Roll Number"))
data=pickle.load(f)
for i in data:
if i[0]==roll:
print(i)
flag=1
break
if flag==0:
print("Data not fund........")
f.close()
No comments:
Post a Comment
for more information please share like comment and subscribe