DELHI PUBLIC SCHOOL, DURGAPUR
CLASS- XII
ASSIGNMENT- 5 (TERM 1-2025-26)
SUBJECT- COMPUTER
TOPIC-BINARY FILES
Binary files:
Binary files are used to store binary data such as images, video files, audio files etc. They store data
in the binary format (0’s and 1’s). In Binary files there is no delimiter for a line. To open files in
binary mode, when specifying a mode, add 'b' to it. Pickle module can be imported to write or read
data in a binary file.
Pickle Module – dump() and load() functions
When you are working with files in computers either you need to understand or convert the files from
one mode to another. Let’s understand in a simple way. If a person who is speaking in Spanish
language and a person who is speaking in the Hindi language in communication then it has meaning
at all. In this process, we need one person who knows both languages.
Similarly in computers also pickle module is doing something exactly (conversion of python objects)
in binary. This process is known as pickling.
It is also known as serialization or flattening or marshalling.
The reverse process is known as unpickling. To do this process you need to import pickle module.
dump() function:
The dump() function or method is usually used with write to object or we can say encoding in python
when the file is accessed in binary mode.
This dump() function accepts two parameters.
• Python Object which needs to be encoded or write
• Data File object in which values of the python object should be written
load() function:
The load() function or method is used to reading data from a binary file or we can say decoding in
python when the file is accessed in binary mode. A variable should be used to assign the values from
the file and decoded.
Write into a binary file:
To write into binary file follow these steps:
• Initiate a python object such as list, dictionary or any other object
• create a file using the open function
• use pickle.dump() method with parameters python object and file object
(CL-XII/CS/Assignment-5/2025-26/Page 1 of 3)
read from binary file:
To read from binary file follow these steps:
• Open file using open() function
• instantiate an object to store data read from a file using load() function
• Print the data
Write data to a Binary File:
Example:
import pickle
e={'Namita':25000,'Manya':30000,'Tanu':20000}
f1=open('emp.dat','wb')
pickle.dump(e,f1)
f1.close()
Read the data from Binary File:
Example:
import pickle
f1=open('emp.dat','rb')
e=pickle.load(f1)
for x in e:
print(x)
f1.close()
Q1. Write a function empadd() which will add a record of employee in a binary file “data.dat”
using list. Data to be add include employee name, employee number. Also write a function
empread() which will read all the records from the file.
def empadd(): # This function is for adding record to a file
f = open("data.dat","wb")
import pickle
d=[]
ename=input("Enter employee name")
en = int(input("Enter employee number"))
temp=[ename, en]
d.append(temp)
d1 = pickle.dump(d,f) # This line is actually writing content to file
f.close() #This line breaks the connection of file handle with file
(CL-XII/CS/Assignment-5/2025-26/Page 2 of 3)
def empread(): #This function is for reading data from file
f=open("data.txt","rb")
import pickle
d = pickle.load(f)
for i in d:
print(d)
f.close()
empadd() # This statement is calling a function to add new record
empread() # This statement is calling a function to read all the records
Important Programs on Text Files
Write a program/function in Python to:
1. Write a function addrecord() to add record of teacher in file “teacher.dat”. Each record should
contain the following data (using list):
1. Teacher Name
2. Teacher designation
3. Teacher Salary
2. Write a function displayrec() to read and display the record of a particular teacher according to the name
entered by the user from a file “teacher.dat” (created above).
3. 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’.
4. A binary file “emp.dat” has structure [EID, Ename, designation, salary] (using dictionary)
i. Write a user defined function CreateEmp() to input data for a record and create a file emp.dat.
ii. Write a function display() in Python to display the detail of all employees whose salary is more than
50000.
5. A binary file “book.dat” has structure [Bookno, 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”.
***************************************
(CL-XII/CS/Assignment-5/2025-26/Page 3 of 3)