0% found this document useful (0 votes)
62 views7 pages

Binary File Class 12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

Binary File Class 12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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)
file_create()
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)
read_data()
[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
[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
[4] Write a menu driven program as following:
1. Insert record
2. Search Record
3. Update Record
4. Display record
5. Delete record
6. Exit
Use cust.dat for this program created in pro3.
import pickle
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()
main_menu()

def delete_rec():
f1 = open("sales.dat","rb")
s=int(input("Enter id to delete:"))
try:
while True:
d = pickle.load(f1)
if s in d:
del d[s]
pickle.dump(d,f1)
except EOFError:
print("Record Deleted.")
f1.close()
main_menu()
Search records with relational operator
[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()
[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()

You might also like