Updating Record in 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.

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

updating-records-from-binary-file

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

file:bin.dat
update-binary-file-in-python

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 update their data as per user requirement. So simply we will use for loop() to iterate each list and then we will perform required operation on each entities of sub list.

Example:

update-data-in-binary-file

 

Explanations: Solution of our problem is divided into six steps. Also we may see that we have successfully updated the third record of our file. Below is the program explanation is given:

Step 1: Here file “bin.dat” is opened in “rb+” mode which is for reading as well as for writing. Also we have load the data of our file in file object named “s“.

Step 2: Here we initialize “final” variable as 0 and asking roll number as input from user whose entry user wants to update 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 of data updation will proceed ie. we will get new updated value from user and as we have found the roll number that user wants to update so 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.

Step 5: Here inside else statement we have set the pointer to 0 position and closed the file after dumping or writing the updated records to our binary file “bin.dat“.

We have set the pointer ( seek() & tell() ) to 0 index as beginning index because we are going to write file from start as we got the updated value from user.

Step 6: Here we have defined read() function just to read all records from our file “bin.dat“. Hence we can see that third record inside our binary file “bin.dat” has been updated successfully.

So, we can observe that the record is updated successfully without performing any changes to other existing data.