Investigatory Questions and Answers
Topic: Binary File Handling in Python
1. Write a Python program to create a binary file `student.dat` containing roll
number, name, and marks of students using `pickle` module.
Answer:
import pickle
f = open('student.dat', 'wb')
n = int(input('Enter number of students: '))
for i in range(n):
roll = int(input('Enter Roll Number: '))
name = input('Enter Name: ')
marks = float(input('Enter Marks: '))
record = [roll, name, marks]
pickle.dump(record, f)
f.close()
2. Write a program to read and display all the records stored in a binary file
`employee.dat`.
Answer:
import pickle
f = open('employee.dat', 'rb')
try:
while True:
record = pickle.load(f)
print(record)
except EOFError:
f.close()
3. Explain with example the difference between `wb`, `rb`, and `ab` modes used
with binary files.
Answer:
- `wb`: Opens file for writing in binary mode (overwrites existing file).
- `rb`: Opens file for reading in binary mode.
- `ab`: Opens file for appending in binary mode (adds data at end).
Example:
open('data.dat','wb') → write new binary file
open('data.dat','rb') → read binary file
open('data.dat','ab') → append to binary file
4. Write a Python program to append new employee records (EmpID, Name,
Salary) into a binary file.
Answer:
import pickle
f = open('employee.dat', 'ab')
empid = int(input('Enter Employee ID: '))
name = input('Enter Name: ')
salary = float(input('Enter Salary: '))
record = [empid, name, salary]
pickle.dump(record, f)
f.close()
5. Write a program to count and display the number of students in a binary file
`student.dat` who have scored more than 75 marks.
Answer:
import pickle
count = 0
f = open('student.dat', 'rb')
try:
while True:
record = pickle.load(f)
if record[2] > 75:
count += 1
except EOFError:
f.close()
print('Number of students scoring > 75:', count)
6. What is the difference between a text file and a binary file in Python? Write
two points and give an example.
Answer:
- Text file stores data in human-readable format (characters). Example: .txt file
- Binary file stores data in machine-readable format (0s and 1s). Example: .dat file
- Text files use strings, binary files can store Python objects using pickle.
7. Write Python code to search for a specific roll number in a binary file
`student.dat` and display the corresponding record.
Answer:
import pickle
rollno = int(input('Enter roll number to search: '))
f = open('student.dat', 'rb')
found = False
try:
while True:
record = pickle.load(f)
if record[0] == rollno:
print('Record Found:', record)
found = True
except EOFError:
f.close()
if not found:
print('Record not found')
8. Write a program to copy the contents of one binary file `data1.dat` into
another binary file `data2.dat`.
Answer:
import pickle
f1 = open('data1.dat', 'rb')
f2 = open('data2.dat', 'wb')
try:
while True:
record = pickle.load(f1)
pickle.dump(record, f2)
except EOFError:
f1.close()
f2.close()
9. Explain the purpose of `pickle.dump()` and `pickle.load()` functions with
examples.
Answer:
- `pickle.dump(object, file)` → Writes Python object into binary file.
- `pickle.load(file)` → Reads Python object from binary file.
Example:
pickle.dump([101, 'Rohit', 90], f)
record = pickle.load(f)
10. Write a Python program to update the salary of an employee in a binary file
`employee.dat` given his employee ID.
Answer:
import pickle, os
fin = open('employee.dat', 'rb')
fout = open('temp.dat', 'wb')
empid = int(input('Enter Employee ID to update: '))
try:
while True:
record = pickle.load(fin)
if record[0] == empid:
record[2] = float(input('Enter new Salary: '))
pickle.dump(record, fout)
except EOFError:
fin.close()
fout.close()
os.remove('employee.dat')
os.rename('temp.dat', 'employee.dat')
11. Write a program to display all employee records from `employee.dat`
where salary is between 30,000 and 50,000.
Answer:
import pickle
f = open('employee.dat', 'rb')
try:
while True:
record = pickle.load(f)
if 30000 <= record[2] <= 50000:
print(record)
except EOFError:
f.close()
12. Why do we need to use binary files in Python when text files already exist?
Explain with two advantages and one limitation.
Answer:
Advantages:
1. Binary files can store complex objects (lists, dictionaries, tuples).
2. They are compact and faster to process.
Limitation:
- Binary files are not human-readable; they need Python/pickle to access.
13. Write Python code to delete a student’s record from `student.dat` (binary
file) based on the roll number.
Answer:
import pickle, os
rollno = int(input('Enter Roll No to delete: '))
fin = open('student.dat', 'rb')
fout = open('temp.dat', 'wb')
try:
while True:
record = pickle.load(fin)
if record[0] != rollno:
pickle.dump(record, fout)
except EOFError:
fin.close()
fout.close()
os.remove('student.dat')
os.rename('temp.dat', 'student.dat')
14. Explain how binary file handling can be useful in storing objects
permanently. Give a real-life example.
Answer:
Binary file handling helps store Python objects permanently on disk using `pickle`. Unlike
text files, objects (lists, dicts, classes) can be stored without converting them to strings.
Example: A school management system storing student records (roll, name, marks) as
objects in a binary file for later retrieval.
15. Write a Python program to read all student records from `student.dat` and
display the highest marks among them.
Answer:
import pickle
f = open('student.dat', 'rb')
highest = 0
try:
while True:
record = pickle.load(f)
if record[2] > highest:
highest = record[2]
except EOFError:
f.close()
print('Highest Marks:', highest)