ByteBoard Notes Notes

Create a binary file with name and roll number and search for a roll number

Computer Science XII · Experiment 11 of 29 · Python

★ CBSE suggested12-CSPython programs
Expt. No.11
Page No.22

Binary file: search by roll number

Aim

To write a Python program that creates a binary file with the name and roll number of students, then searches for a roll number and displays the name, or a message if it is not found.

Concepts used

Algorithm

  1. create_file(): open students.dat in "wb" mode; input each student and write [roll, name] with pickle.dump().
  2. search(roll): open the file in "rb" mode; load records one by one with pickle.load() until EOFError.
  3. If a record's roll number matches, display the name and stop.
  4. If no record matched, display "not found".

Program

# Binary file: store students and search by roll number import pickle def create_file():    n = int(input("How many students? "))    with open("students.dat", "wb") as f:        for i in range(n):            roll = int(input("Roll number: "))            name = input("Name       : ")            pickle.dump([roll, name], f) def search(roll):    found = False    with open("students.dat", "rb") as f:        try:            while True:                rec = pickle.load(f)                if rec[0] == roll:                    print("Name:", rec[1])                    found = True                    break        except EOFError:            pass    if not found:        print("Roll number", roll, "not found") create_file()r = int(input("Enter the roll number to search: "))search(r)

A marked line continues the line above it. In your file, write it on the same line.

Output

Run 1
$ python binary_file_search.py
How many students? 3 Roll number: 101 Name : Aman Roll number: 102 Name : Bhavya Roll number: 103 Name : Chirag Enter the roll number to search: 102 Name: Bhavya
Run 2
$ python binary_file_search.py
How many students? 3 Roll number: 101 Name : Aman Roll number: 102 Name : Bhavya Roll number: 103 Name : Chirag Enter the roll number to search: 110 Roll number 110 not found

✓ Printed by running this program, not typed by hand.

Result

Roll number 102 was found and the name Bhavya was displayed. Roll number 110 was not in the file, so the "not found" message was shown after every record had been read.

Viva voce: what the examiner may ask

01What does pickle.dump() do?

It converts a Python object (here a list) into bytes and writes it to a binary file. This is called pickling.

02Why is EOFError caught?

pickle.load() raises EOFError when there are no more records; catching it ends the reading loop.

03Why must the file be opened in "wb" and "rb" modes?

Pickled data is bytes, so the file must be opened in binary mode.

Variations for the lab test

  1. Display all the records in the file.
  2. Search by name instead of roll number.
  3. Count the number of records in the file.
! Common mistake Opening the file in "r" mode instead of "rb" makes pickle.load() fail, because it needs bytes, not text.
Back to the 12-CS index