0% found this document useful (0 votes)
29 views

Binary File Programs

The document contains multiple Python programs for handling binary files, including creating, reading, updating, and searching records for toys, students, books, employees, products, and customers. Each section provides a specific function to manage data structures defined for each type of record, utilizing the pickle module for serialization. The programs demonstrate various file operations such as appending records, searching by criteria, and displaying filtered results based on specific conditions.

Uploaded by

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

Binary File Programs

The document contains multiple Python programs for handling binary files, including creating, reading, updating, and searching records for toys, students, books, employees, products, and customers. Each section provides a specific function to manage data structures defined for each type of record, utilizing the pickle module for serialization. The programs demonstrate various file operations such as appending records, searching by criteria, and displaying filtered results based on specific conditions.

Uploaded by

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

BINARY FILE HANDLING PROGRAMS

1. A binary file “Toy.dat” has structure [TID, Toy, Status,MRP].


i. Write a user defined function CreateFile() to input data for a record and add to
"Toy.dat"
ii. Write a function OnOffer() in Python to display the detail of those Toys, which has
status as “ON OFFER” from "Toy.dat" file.

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()

2. A binary file “student.dat” has structure [rollno, name, marks].


i. Write a user defined function insertRec() to input data for a student and add to
student.dat.
ii. Write a function searchRollNo( r ) in Python which accepts the student’s rollno as
parameter and searches the record in the file “student.dat” and shows the details of student
i.e. rollno, name and marks (if found) otherwise shows the message as ‘No record found’.

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)

3. A binary file “Book.dat” has structure: [BookNo, Book_Name, Author,Price].


i. Write a user defined function CreateFile() to input data for a record and add to
Book.dat.
ii. Write a function CountRec(Author) in Python which accepts the Author name as
parameter and count and return number of books by the given Author are stored in the
binary file “Book.dat”.

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))

4. A binary file “STUDENT.DAT” has structure (admission_number, Name, Percentage).


Write a function countrec() in Python that would read contents of the file
“STUDENT.DAT” and display the details of those students whose percentage is above 75.
Also display number of students scoring above 75%.
import pickle
def countrec():
fo=open("STUDENT.DAT","rb")
count=0
print(“Student details whose percentage > 75:”)
try:
while True:
rec=pickle.load(fo)
if rec[2]>75:
count+=1
print(rec)
except EOFError:
fo.close()
print(“Total no of students scored 75 % above are:”, count)
countrec():

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))

9. i) A binary file “emp.dat” has structure (EID, Ename, designation,salary). Write a


function to add more records of employees in existing file emp.dat.
ii. Write a function Show() in Python that would read detail of employee from file
“emp.dat” and display the details of those employee whose designation is “Salesman”.

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()

10. A binary file student.dat has structure (rollno,name,class,percentage). Write a program


to updating a record in the file requires roll number to be fetched from the user whose
name is to be updated.
Import pickle
def update(rollno):
fo=open("student.dat ","rb+")
try:
while True:
rpos=fo.tell()
record=pickle.load(fo)
rec=list(record)
if rec[0]==rollno:
name=input(“Enter name to update for the student:”)
rec[1]=name
r=tuple(rec)
fo.seek(rpos)
pickle.dump(r,fo)

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()

You might also like