Deleting Records From Binary File in Python
Delete Records from Binary file in Python: Binary files are always written in structure form and later it is converted to byte stream before storing in a binary file. By structure from I mean to say in formats like lists, tuple, sets, dictionary etc.
How to delete records in Binary File: There is no pre-defined function available in python for deleting records in a binary file in python. We will define our logic or function to solve our problem of deleting records.
Important: We will delete the record from our existing file “bin.dat” which already have some data written to it. So, below is our raw file with data.
file: “bin.dat”
We will update the information of student with roll number ‘1602’
Problem Solving Approach: The problem-solving approach will be that we will use one empty “temp.dat” to store all records. We will write all records available in “bin.dat” file in “temp.dat” file except the record user wants to delete and later we will rename that “temp.dat” file to “bin.dat” file and also we will delete the old “bin.dat” file. Hence, in this way, we can delete records from a binary file. Let’s have a look at the program:
Program: Delete Records from Binary file in Python:
Explanations: The solution to our problem is divided into seven easy steps. Also, we can see that we have successfully deleted 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. Read records from binary file.
- Step A: In this block we have defined our complete delete method definition and sub divided the whole process of execution in four sub parts (2,3,4,5).
- Step 2: Here we have opened “bin.dat” in “rb” mode ie. reading mode. Also, a temporary file with the name “temp.dat” in “wb” ie. in write mode is opened.
- We have also declared an empty list rec[] which we will use later and also storing roll number from user input in “rn” variable that he wants to delete record. We are also reading the file using load method and storing that in file object “s“.
- Step 3: After getting the input roll number that the user wants to delete, at step 3 each sublist 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.
- So, if the value of “rn” matches with any record then the flow of control statement instead of entering to if condition it will enter to else: part and the roll number that user wants to delete will be ignored/skipped to append on rec[] list.
Rest all records enter to if condition and append to rec[] list. - Step 4: Here in fourth step, we are writing all data to “temp.dat” that was appended to rec[] list and also we have closed “temp.dat” file as well as “bin.dat” file.
- Step 5: Here we have used the os module to rename as well to delete the file.
- Step 6: This is the read2() method defined to read the records after successful deletion of the record.
- Step 7: Here we have called our defined functions as per requirement.
Hence, In this way, we can delete records from Binary File.
Other guides::