Editor

Python Program to Search a Roll Number in a Binary File

Intermediate · File handling · CBSE class 12 practical list

A binary file stores Python objects such as lists and dictionaries exactly as they are, using Python's pickle module, instead of as plain text. This program creates a binary file of student records (roll number and name), then searches it for a roll number and prints the name, or a message when the roll number is not in the file. It is a standard CBSE class 12 practical program.

Program

binary_file_search.py
import pickle
# Make a sample file to work on (in your practical file, this file already exists)
with open("students.dat", "wb") as f:
pickle.dump([101, "Aarav"], f) # each record is [roll number, name]
pickle.dump([102, "Diya"], f)
pickle.dump([103, "Kabir"], f)
pickle.dump([104, "Meera"], f)
def search(roll):
found = False
with open("students.dat", "rb") as f:
try:
while True:
record = pickle.load(f) # reads the next record
if record[0] == roll:
print("Roll number", roll, "found, name:", record[1])
found = True
break
except EOFError: # raised when there are no more records
pass
if not found:
print("Roll number", roll, "not found in the file")
search(103) # or: search(int(input("Enter roll number to search: ")))
search(110)
Output
Roll number 103 found, name: Kabir
Roll number 110 not found in the file

How it works

  1. Files opened with "wb" (write binary) and "rb" (read binary) hold raw bytes, which is what pickle needs.
  2. Each pickle.dump(record, f) call writes one record, so the file ends up holding four records one after another.
  3. pickle.load(f) reads one record per call, and the while True loop keeps reading until the file runs out and Python raises EOFError, which the except block catches.
  4. When the roll number matches, the name is printed and break stops the search early.
  5. For roll number 110 nothing matches, so found is still False after the loop and the not found message is printed.

Another way to write it

Storing all the records in one list

Storing all the records in one list
import pickle
# Make a sample file to work on (in your practical file, this file already exists)
students = [[101, "Aarav"], [102, "Diya"], [103, "Kabir"]]
with open("students.dat", "wb") as f:
pickle.dump(students, f) # the whole list in one go
with open("students.dat", "rb") as f:
students = pickle.load(f) # and back in one go
roll = 102
for record in students:
if record[0] == roll:
print("Roll number", roll, "found, name:", record[1])
break
else:
print("Roll number", roll, "not found")
Output
Roll number 102 found, name: Diya

Dumping the whole list once means a single pickle.load reads everything back, with no EOFError loop. The else of a for loop runs only when the loop ends without break, that is, when nothing matched.

Try it yourself

Add marks as a third value in every record and print the marks along with the name when the roll number is found.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises