Binary_File_Handling
Binary_File_Handling
def addrec():
l1=[]
f=open("teacher.dat","wb")
ch="y"
while ch=="y":
tname=input("Enter teacher's name")
tdes=input("Enter designation")
tsal=int(input("Enter salary"))
l1=[tname,tdes,tsal]
pickle.dump(l1,f)
ch=input("Do you want to continue(y/n)")
DISPLAY RECORDS
def displayrec():
f=open("teacher.dat","rb")
while True:
try:
data=pickle.load(f)
print(data)
except Exception:
break
SEARCH RECORD
def searchrec():
f=open("teacher.dat","rb")
tname=input("Enter teacher's name")
found=0
while True:
try:
data=pickle.load(f)
if data[0]==tname:
print("Record Found")
print("Teacher name-",data[0])
print("Teacher designation-",data[1])
print("Teacher salary-",data[2])
found=1
except Exception:
break
if found==0:
print("Record Not Found")
UPDATE RECORD
f=open("teacher.dat","rb") found=1
L=[] if found==0:
while True: print("record not found")
try: else:
data=pickle.load(f) f=open('teacher.dat','wb')
L.append(data) for i in L:
except Exception: pickle.dump(i,f)
break f.close()
found=0
tname=input("Enter teacher's name")
for i in L:
if i[0]==tname:
option=input("Which record do you need to
update.\n1.teacher name\n2.teacher
designation\n3.teacher salary")
if option=="1":
tname=input("Enter new name")
i[0]=tname
elif option=="2":
tdes=input("Enter the new designation")
i[1]=tdes
elif option=="3":
tsalary=int(input("Enter the new salary"))
DELETE RECORD
def deleterec():
f=open("teacher.dat","rb")
L=[]
tname=input("Enter teacher's name to
delete")
found=0 if found==0:
while True: print("record not found")
try: else:
data=pickle.load(f) print(L)
if data[0]!=tname:
L.append(data) f=open('teacher.dat','wb')
found=1 for i in L:
except Exception: pickle.dump(i,f)
break f.close()
COUNT RECORD
def countrec():
count=0
f=open("teacher.dat","rb")
while True:
try:
data=pickle.load(f)
count+=1
except Exception:
break
print("The number of data in the file is",count)
BINARY FILE HANDLING- PROGRAM 2
Write a program to enter the following records in a binary file:
ItemNo Integer, Item_Name string and Price integer in the
form of a dictionary. The number of records to be entered
should be accepted from the user.
Include the following functions:
• Add Records
• Read and display records
• Search records
• Update records
• Count records
• Delete records