Binary File Handling in Python class 12
Topics Covered
- Types of Files
- Binary Files
- Pickle Module
- Method dump() & load()
- Sample Program
Introduction to Files
Python that accept the input manipulate it and display the output. A file is a named location on a secondary storage media where data are permanently stored for later access.
Types of Files
There are two types of data files
- text file
- binary file.
Text Files
A text file consists of human readable characters, which can
be opened by any text editor. they are
stored in sequence of bytes consisting of 0s and 1s. In ASCII, UNICODE or any
other encoding scheme
Binary Files
binary files are made up of non-human readable characters
and symbols, which require specific programs to access its contents.
Binary files are also stored in terms of bytes (0s and 1s), but
unlike text files, these bytes do not represent the ASCII values of characters.
Now we are going to discuss about Binary files
Pickle Module
Python considers everything as an object. So, all data types
including list, tuple, dictionary, etc. are also considered as objects. Python
provides a module called Pickle. The module Pickle is used for serializing and
de-serializing any Python object structure. Pickling is a method of preserving
food items by placing them in some solution, which increases the shelf life.
The pickle module provides two methods - dump() and load() to work with binary
files for pickling and unpickling, respectively.
Serialization is the process of transforming data or an
object in memory (RAM) to a stream of bytes called byte streams. These byte
streams in a binary file can then be stored in a disk or in a database or sent
through a network. Serialization process is also called pickling.
De-serialization or unpickling is the inverse of pickling process where a byte
stream is converted back to Python object.
The dump() method
This method is used to convert (pickling) Python objects for
writing data in a binary file. The file in which data are to be dumped, needs
to be opened in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)
Eg:
Pickling data in Python
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()
The load() method
This method is used to load (unpickling) data from a binary
file. The file to be loaded is opened in binary read (rb) mode. Syntax of
load() is as follows:
Store_object = load(file_object)
Unpickling data in Python
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Program to create a binary file with some data
import pickle
fh=open("ram1.bat","wb")
data={}
conf="y"
while conf=="y":
name=input("Enter Name:")
age=int(input("Enter Age:"))
Class=int(input("Enter the Class:"))
data["NAME"]=name
data["Stu_Age"]=age
data["Class"]=Class
pickle.dump(data,fh)
print("Stored!!!!!!")
conf=input("Do you want to cont.--Yes or No:")
fh.close()
To perform basic operations on a binary file using pickle module
# Program to write and read employee records in a binary
file
import pickle
print("WORKING WITH BINARY FILES")
bfile=open("empfile.dat","ab")
recno=1
print ("Enter Records of Employees")
print()
#taking data from user and dumping in the file as list
object
while True:
print("RECORD
No.", recno)
eno=int(input("\tEmployee number :
"))
ename=input("\tEmployee Name : ")
ebasic=int(input("\tBasic Salary :
"))
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
print("\tTOTAL
SALARY : ", totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,bfile)
ans=input("Do
you wish to enter more records (y/n)? ")
recno=recno+1
if ans.lower()=='n':
print("Record
entry OVER ")
print()
break
# retrieving the size of file
print("Size of binary file (in
bytes):",bfile.tell())
bfile.close()
# Reading the employee records from the file using load()
module
print("Now reading the employee records from the
file")
print()
readrec=1
try:
with open("empfile.dat","rb") as bfile:
while True:
edata=pickle.load(bfile)
print("Record
Number : ",readrec)
print(edata)
readrec=readrec+1
except EOFError:
pass
bfile.close()
Search Related Tags
- amongst which of the following is / are the method used to unpickling data from a binary file?
- dump and load in python pickle
- pickle.load in python
- pickle.dump in python
- python pickle example
- pickle module in python
- pickling and unpickling in python
- how to append data in binary file in python using pickle
- binary file program in python class 12
- binary files in python class 12 notes
- binary file questions in python class 12
- computer science syllabus class 12 2023-24
- class 12 computer science syllabus 2023-24 pdf
- computer science deleted syllabus class 12 2023
- computer science syllabus class 12 2023 pdf
- binary file handling in python class 12 questions and answers