Binary File Programs
Binary File Programs
import pickle
def Createfile():
fo=open("Toy.dat","ab")
TID=input("Enter Toy ID:")
Toy=input("Enter toy name:")
Status=input("Enter status of a toy:")
MRP=int(input("Enter MRP of a Toy:"))
rec=[TID,Toy,Status,MRP]
pickle.dump(rec,fo)
fo.close()
Createfile()
def OnOffer():
fo=open("Toy.dat","rb")
S=input("Enter status of a toy to search:")
found=0
print("The detail of the Toys, which has status as “ON OFFER”")
try:
while True:
rec=pickle.load(fo)
if rec[2].upper()=='ON OFFER':
found=1
print(rec)
except EOFError:
if found==0:
print("No record found")
fo.close()
OnOffer()
import pickle
def insertRec():
fo=open("student.dat","ab")
rollno=input("Enter rollno of a student:")
name=input("Enter name of a student:")
marks=int(input("Enter marks of a student:"))
rec=[rollno,name,marks]
pickle.dump(rec,fo)
fo.close()
insertRec()
def searchRollNo( r):
fo=open("student.dat","rb")
found=0
print(“Student Details”)
try:
while True:
rec=pickle.load(fo)
if rec[0]==r:
found=1
print(rec)
except EOFError:
if found==0:
print("No record found")
fo.close()
rollno=int(input(“Enter roll no to search:”))
searchRollNo( rollno)
import pickle
def CreateFile():
fo=open("Book.dat","ab")
BookNo=int(input("Enter BookNo:"))
Book_Name=input("Enter name of a book:")
Author=input("Enter author of a book:")
Price=int(input("Enter the Price of the book:"))
rec=[BookNo,Book_Name,Author,Price]
pickle.dump(rec,fo)
fo.close()
CreateFile()
def CountRec(Author):
fo=open("Book.dat","rb")
count=0
try:
while True:
rec=pickle.load(fo)
if rec[2]==Author:
count+=1
print(rec)
except EOFError:
fo.close()
return count
Author=input("Enter Author name to search:")
print("Total no of books in the given author:",CountRec(Author))
5. Write a python program to append a new records in a binary file –“student.dat”. The
record can have Rollno, Name and Marks.
import pickle
def appendRec():
fo=open("student.dat","ab")
Rollno=input("Enter rollno of a student:")
Name=input("Enter name of a student:")
Marks=int(input("Enter marks of a student:"))
rec=[Rollno,Name,Marks]
pickle.dump(rec,fo)
fo.close()
appendRec()
6. Write a Program in Python that defines and calls the following user defined functions:
(i) add() – To accept and add data of an employee to a binary file ”EMP.dat‟. Each record
consists of a list with field elements as EId, EName and ESal to store Emp id, Empname
and Empsalary respectively.
(ii) search()- To display the records of the employee salary is more than 10000.
import pickle
def add():
fo=open("EMP.dat","ab")
EId=int(input("Enter employee ID:"))
EName=input("Enter name of an Employee:")
ESal=int(input("Enter the salary:"))
rec=[EId,EName,ESal]
pickle.dump(rec,fo)
fo.close()
add()
def search():
fo=open("EMP.dat ","rb")
found=0
try:
while True:
rec=pickle.load(fo)
if rec[2]>10000:
found=1
print(rec)
except EOFError:
if found==0:
print(“No record found”)
fo.close()
search()
7. Write a program in python that defines and calls the following user defined functions:
Add_rating() – To read product name and rating from the user and store the same into the
binary file “product.dat”
Display() – To read the file “product.dat” and display the products where the rating is
above 10.
import pickle
def Add_rating():
fo=open("product.dat","ab")
pname=input("Enter Product Name:")
prating=int(input("Enter the Product rating:"))
rec=[pname,prating]
pickle.dump(rec,fo)
fo.close()
Add_rating()
def Display():
fo=open("product.dat ","rb")
found=0
try:
while True:
rec=pickle.load(fo)
if rec[1]>10:
found=1
print(rec)
except EOFError:
if found==0:
print(“No record found”)
fo.close()
Display()
8. Write a program in python that defines and calls the following user defined functions:
(i) insertData() – To accept and add data of a customer and add it to a binary file
customerData.dat‟. Each record should contain a list consisting customer name, mobile no,
date of Purchase, item purchased.
(ii) frequency (name) – To accept the name of a customer and search how many times the
customer has purchased any item. The count and return the number.
import pickle
def insertData():
fo=open("customerData.dat","ab")
cname=input("Enter customer name:")
mobileno=int(input("Enter mobile number:"))
dop=input("Enter date of purchase:")
itempurchased= input("Enterthe item purchased:"))
rec=[cname, mobileno, dop, itempurchased]
pickle.dump(rec,fo)
fo.close()
insertData()
def frequency(name):
fo=open("customerData.dat ","rb")
count=0
try:
while True:
rec=pickle.load(fo)
if rec[0]==name:
count+=1
except EOFError:
fo.close()
return count
cname=input("Enter customer name to search:")
print("Total no of items purchased by the given customer are:”, frequency(cname))
import pickle
def add():
fo=open("emp.dat","ab")
while True:
EID=int(input("Enter employee ID:"))
Ename=input("Enter name of an Employee:")
designation=input(“Enter Employee designation:”)
salary=int(input("Enter the salary:"))
rec=(EID,Ename,designation,salary)
pickle.dump(rec,fo)
ch=input(“Do you want to enter more records?”)
if ch in ‘Nn’:
break
fo.close()
add()
def Show():
fo=open("emp.dat ","rb")
found=0
print(“Details of those employees whose designation is Salesman are:”)
try:
while True:
rec=pickle.load(fo)
if rec[2]==”Salesman”:
found=1
print(rec)
except EOFError:
if found==0:
print(“No record found”)
fo.close()
Show()
except EOFError:
fo.close()
rollno=int(input(“Enter rollno to update student name:”))
update(rollno)
11. Consider a binary file emp.dat having records in the form of dictionary. E.g {eno:1,
name:”Rahul”, sal: 5000} write a python function to display the records of above file for
those employees who get salary between 25000 and 30000.
import pickle
def display():
fo=open("emp.dat ","rb")
found=0
print(“Details of those employees whose designation is Salesman are:”)
try:
while True:
rec=pickle.load(fo)
if rec[2]==”Salesman”:
found=1
print(rec)
except EOFError:
if found==0:
print(“No record found”)
fo.close()
Show()