Python Binary File Handling Notes – Class 12 CS (083)
Python Binary File Handling Notes for Class 12 CS (083) covering binary file operations, pickle module, dump(), load(), file modes, and read, write, search, append, and update operations.
Binary File is an important topic in the File Handling in Python chapter of Class 12 Computer Science (083) and is frequently asked in board examinations and practicals. These CBSE-aligned notes cover all essential concepts in sequence. Each topic is explained in a simple manner with practical programs to help students build strong concepts, improve problem-solving skills, and perform confidently in examinations.
Binary Files
- Binary files are stored as bytes (0s and 1s), but these bytes do not represent ASCII character values.
- Instead, they store actual data such as images, audio, video, compressed files, and executable files.
- Binary files are not human-readable.
- Opening a binary file in a text editor displays unreadable or garbage values.
- Special software is required to read or write binary file contents.
- Binary files are stored as a sequence of bytes in the computer.
- Even a single bit change can corrupt the file and make it unusable.
- Errors in binary files are difficult to detect and fix because the content is not readable.
- Python can be used to read and write both text files and binary files.
- pickle module is used to read and write data in binary files.
The Pickle Module
- In Python, everything is treated as an object (list, tuple, dictionary, etc.).
- To store and retrieve such object structures, Python provides the Pickle module.
- Pickle is used for:
- Serializing (pickling) → converting objects into byte stream
- De-serializing (unpickling) → converting byte stream back to objects
Serialization:
- Converts data/object in memory into byte stream
- Stored in binary file / database / network
- Also called pickling
De-serialization:
- Converts byte stream back into Python object
- Also called unpickling
Binary File Handling using pickle module
- The pickle module is used to store and retrieve Python objects in binary files.
- The pickle module must be imported before working with binary files.
- Syntax:
import pickle - pickle module provides two methods:
- dump() Method (pickling) – Used to save (write) Python objects into a binary file.
- load() Method (unpickling) – Used to retrieve (read) Python objects from a binary file.
dump() Method (Pickling)
- Used to convert Python objects into binary form and store in file
- File must be opened in binary write mode (wb)
- Syntax:
pickle.dump(data_object, file_object)
Example: dump()
import pickle
listvalues = [1, “Geetika”, ‘F’, 26]
fileobject = open(“mybinary.dat”, “wb”)
pickle.dump(listvalues, fileobject)
fileobject.close()
load() Method (Unpickling)
- Used to retrieve data from binary file
- File must be opened in binary read mode (rb)
- Syntax:
object = pickle.load(file_object)
Example: load()
import pickle
print(“The data that were stored in file are:”)
fileobject = open(“mybinary.dat”, “rb”)
objectvar = pickle.load(fileobject)
fileobject.close()
print(objectvar)
Output
The data that were stored in file are:
[1, ‘Geetika’, ‘F’, 26]
Example: File Handling using Pickle (Binary File Operations)
Example: Writing and Reading Employee Records
import pickle
print(“WORKING WITH BINARY FILES”)
bfile = open(“empfile.dat”, “ab”)
recno = 1
print(“Enter Records of Employees”)
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
edata = [eno, ename, ebasic, allow, totsal]
pickle.dump(edata, bfile)
ans = input(“Do you wish to enter more records (y/n)? “)
recno += 1
if ans.lower() == ‘n’:
print(“Record entry OVER”)
break
print(“Size of binary file (in bytes):”, bfile.tell())
bfile.close()
print(“Now reading the employee records from the file”)
readrec = 1
try:
with open(“empfile.dat”, “rb”) as bfile:
while True:
edata = pickle.load(bfile)
print(“Record Number:”, readrec)
print(edata)
readrec += 1
except EOFError:
pass