Search Records From Binary File in Python

Note: Binary files are always written in structure form and later it is converted to byte stream before storing to binary file. By structure from I mean to say in formats like lists, tuple, sets, dictionary etc.

Searching Records in Binary File: There is no any pre-defined function available in python for searching records in binary file in python. We will define our logic or function to solve our problem of updating records.

search-records-in-binary-file

Important: We will search records from our existing file “bin.dat” having some data written to it. So, below is our raw file with data.

file: “bin.dat
search-records-from-binary-file

We will update the information of student with roll number ‘1602’

Problem Solving Approach: Binary file always stores multiple data in nested list or any nested structure form, so that our file “bin.dat” is storing. Inside our list there is sub lists are also there and we have to search from that sub list. So simply we will use for loop() to iterate each sub list and then we will perform required operation on each entities of sub list.

Example: 

search-records-from-binary-file

Explanations: Solution of our problem is divided into four easy steps. Also we can see that we have successfully found the third record of our file. Below the program explanation is given:

Step 1: Here we have defined read() function, just to check the records available in our binary file.

Step A: Here this is our main search() function that we have defined to solve our problem. Inside this, we have further sub divided the problem solution in three steps (2,3,4).

Step 2: Here file “bin.dat” is opened in “rb” mode which is for reading binary file. Also we have load the data of our file in file object named “s“ and defined initialized “final” variable with value 0. And asking roll number as input from user whose entry user wants to search and we are storing that input value in “rn” variable.

Step 3: Here for loop we used to iterate over each sub-list of list. So after successful execution of step 2, next execution will enter to for loop. Here each sub list is iterated and assign to “i” one by one. Now, if condition will check for, is the value inside “rn” matches with value available at i[0] index ?

Important: Actually “i” is storing the sub-list ie. [1600,”Raj”,20]. so here value of i[0] is 1600. Hence each roll number is checked.

If any of roll number is matched with the value. Then the next step is to display the information available in our file to user. We have found the roll number that user wants to search so now we will also break the for loop and set the value of “final” variable to 1.

Step 4: At this step we have again set the value of “final” variable to 0 and print message as no record found. This is because in step 3 user will only enter inside if condition only if value of rn is matched with any of record available inside sub-list else execution jump to step 4.

Hence this is the way to search records from a binary file in python.