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

Binary_File_Handling

The document discusses binary file handling in Python, focusing on the concepts of pickling (serialization) and unpickling (deserialization) for storing and retrieving data structures. It provides examples of functions for adding, displaying, searching, updating, counting, and deleting records in binary files, specifically for teacher and item records. The document emphasizes the use of the pickle module and the importance of opening files in the correct binary mode for these operations.

Uploaded by

dabeer423
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary_File_Handling

The document discusses binary file handling in Python, focusing on the concepts of pickling (serialization) and unpickling (deserialization) for storing and retrieving data structures. It provides examples of functions for adding, displaying, searching, updating, counting, and deleting records in binary files, specifically for teacher and item records. The document emphasizes the use of the pickle module and the importance of opening files in the correct binary mode for these operations.

Uploaded by

dabeer423
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

BINARY FILE HANDLING

Handling the file in the way computer understands


Binary files
Example: Image files, audio files,Video files.
• We can open binary files in the normal text editor but
we cannot read it.
• That’s because all binary files will be encoded in the
binary format, which can be understood, only by a
computer or a machine.
• In binary files, there is no delimiter at the end of the
file.
• No need to translate them.
Pickling and unpickling

Pickling or Serialization. refers to the process of converting


structure (such as list or dictionary) to a byte stream before
writing to the file.

Structure Pickling Byte stream


Unpickling or de- Serialization refers to the process of converting
the byte stream back to the orginal structure.

Byte stream unpickling Structure


dump() method
• pickle.dump
• Used to write the object in a file .
• The file in which data are to be dumped, needs to
be opened in binary write mode (wb).
• Syntax: pickle.dump(structure, fileobject)
• Structure is any sequence of python,
load() method
• pickle.load()
• Used to read the data from a file .
• The file to be loaded is opened in binary read
(rb) mode.
• Syntax: pickle.load(fileobject)
Write a function writedata() to write employee
records in a binary file named “emp.dat” The
structure of “emp.dat” is [empno, empname,
empsal].
BINARY FILE HANDLING PROGRAM-1
QUESTION:
Write a function addrec() to add record of teacher in file
“Teacher.dat”. Each record should contain the following data
(using list):
Teacher Name
Teacher Designation
Teacher Salary
Also write a function searchrecord() to read and display the
record of a particular teacher according to the name entered by
the user from a file “Teacher.dat”, add a function
countrecord() to count the number of records entered and
updaterecord() to update a specific record’s information and
also a function deleterecord() to delete a record from the list.
ADD RECORD

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

You might also like