This article provides you Important Binary File programs class 12, that you can attach in your practical file. Observe these practicals programs and download the code is given at the bottom of the article.
Topics Covered
Important Binary File programs class 12
Write a python program to do the following:
Simple reading writing operations – Binary File programs class 12
[1] Create a binary file student.dat to hold students’ records like Rollno., Students name, and Address using the list. Write functions to write data, read them, and print on the screen.
import pickle
rec=[]
def file_create():
f=open("student.dat","wb")
rno = int(input("Enter Student No:"))
sname = input("Enter Student Name:")
address = input("Enter Address:")
rec=[rno,sname,address]
pickle.dump(rec,f)
def read_read():
f = open("student.dat","rb")
print("*"*78)
print("Data stored in File....")
rec=pickle.load(f)
for i in rec:
print(i)
file_create()
read_data()
Store tuple data into binary file – Binary File programs class 12
[2] Write a program to read and write programming languages stored in the tuple to binary file PL.dat.
import pickle
def bin2tup():
f = open("PL.dat","wb")
t = ('C','C++','Java','Python')
pickle.dump(t,f)
f.close()
f = open("PL.dat","rb")
d = pickle.load(f)
print("PL1:",d[0])
print("PL2:",d[1])
print("PL3:",d[2])
print("PL4:",d[3])
f.close()
bin2tup()
Store dictionary data into binary file – Binary File programs class 12
[3] Write a program to store customer data into a binary file cust.dat using a dictionary and print them on screen after reading them. The customer data contains ID as key, and name, city as values.
import pickle
def bin2dict():
f = open("cust.dat","wb")
d = {'C0001':['Subham','Ahmedabad'],
'C0002':['Bhavin','Anand'],
'C0003':['Chintu','Baroda']}
pickle.dump(d,f)
f.close()
f = open("cust.dat","rb")
d = pickle.load(f)
print(d)
f.close()
bin2dict()
Menu Driven program with all operations – Binary File programs class 12
[4] Write a menu driven program as following:
- Insert record
- Search Record
- Update Record
- Display record
- Exit
Use cust.dat for this program created in pro3.
import pickle
import os
def main_menu():
print("1. Insert a record")
print("2. Search a record")
print("3. Update a record")
print("4. Display a record")
print("5. Delete a record")
print("6. Exit")
ch = int(input("Enter your choice:"))
if ch==1:
insert_rec()
elif ch==2:
search_rec()
elif ch==3:
update_rec()
elif ch==4:
display_rec()
elif ch==5:
delete_rec()
elif ch==6:
print("Have a nice day!")
else:
print("Invalid Choice.")
def insert_rec():
f = open("sales.dat","ab")
c = 'yes'
while True:
sales_id=int(input("Enter ID:"))
name=input("Enter Name:")
city=input("Enter City:")
d = {"SalesId":sales_id,"Name":name,"City":city}
pickle.dump(d,f)
print("Record Inserted.")
print("Want to insert more records, Type yes:")
c = input()
c = c.lower()
if c not in 'yes':
break
main_menu()
f.close()
def display_rec():
f = open("sales.dat","rb")
try:
while True:
d = pickle.load(f)
print(d)
except Exception:
f.close()
main_menu()
def search_rec():
f = open("sales.dat","rb")
s=int(input("Enter id to search:"))
f1 = 0
try:
while True:
d = pickle.load(f)
if d["SalesId"]==s:
f1=1
print(d)
break
except Exception:
f.close()
if f1==0:
print("Record not found...")
else:
print("Record found...")
main_menu()
def update_rec():
f1 = open("sales.dat","rb")
f2 = open("temp.dat","wb")
s=int(input("Enter id to update:"))
try:
while True:
d = pickle.load(f1)
if d["SalesId"]==s:
d["Name"]=input("Enter Name:")
d["City"]=input("Enter City:")
pickle.dump(d,f2)
except EOFError:
print("Record Updated.")
f1.close()
f2.close()
os.remove("sales.dat")
os.rename("temp.dat","sales.dat")
main_menu()
def delete_rec():
f1 = open("sales.dat","rb")
f2 = open("temp.dat","wb")
s=int(input("Enter id to delete:"))
try:
while True:
d = pickle.load(f1)
if d["SalesId"]!=s:
pickle.dump(d,f2)
except EOFError:
print("Record Deleted.")
f1.close()
f2.close()
os.remove("sales.dat")
os.rename("temp.dat","sales.dat")
main_menu()
main_menu()
Search records with relational operator – Binary File programs class 12
[5] Write a function to write data into binary file marks.dat and display the records of students who scored more than 95 marks.
import pickle
def search_95plus():
f = open("marks.dat","ab")
while True:
rn=int(input("Enter the rollno:"))
sname=input("Enter the name:")
marks=int(input("Enter the marks:"))
rec=[]
data=[rn,sname,marks]
rec.append(data)
pickle.dump(rec,f)
ch=input("Wnat more records?Yes:")
if ch.lower() not in 'yes':
break
f.close()
f = open("marks.dat","rb")
cnt=0
try:
while True:
data = pickle.load(f)
for s in data:
if s[2]>95:
cnt+=1
print("Record:",cnt)
print("RollNO:",s[0])
print("Name:",s[1])
print("Marks:",s[2])
except Exception:
f.close()
search_95plus()
Note: More programs can be one using relational operators and search with range and all.
[6] Write a function to count records from the binary file marks.dat.
import pickle
def count_records():
f = open("marks.dat","rb")
cnt=0
try:
while True:
data = pickle.load(f)
for s in data:
cnt+=1
except Exception:
f.close()
print("The file has ", cnt, " records.")
count_records()
So in this article I have tried tuple, list and dictionary with binary functions. Hope you like this. Do more practice with relevant examples and enjoy coding!!!!!
Share this article with your friends.
Thank you for visiting my blog. If you need any more practical contents feel free to ask in comment section.