Write Multiple Data to 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.

python-video-tutorials

Very ImportantIf we are writing record of one student (roll no, name, class, section etc.) in binary file. As discussed already that we will store all these records in structure form ie. lists, tuples etc. So, in case when we have to write records of more than one student in same binary file then in that case what we will do ?

“Yes” we will use append() method to append data to list. But do we append data of other student to same list ? Actually we cannot append data of other student to same list as it is not the better way. Here we will use nested list concept.

writing-multiple-data-to-binary-file


Write multiple data: 
There is no any built in function is available to write multiple data to binary file. We will create our own logic and program to solve this problem. Also to keep in mind that we are going to write binary file using ‘wb‘ mode which means if file with same name already existing in the system than it will over write else it will create new file with given name.

Problem Solving Approach: Basically our requirement is that we will keep on asking to user to enter records till he was not done with entering records. And when he user was done with entering records, then he may choose “No” option to stop writing records.

Example:

write-multiple-data-to-binary-file

Explanation: The above image showing the solution of our problem to enter more than one records in our binary file. The coding approach has been explained in step by step approach below.

Step 1: We have defined write() function and inside write function at step 1 a file object named as “file” has been created with opening binary file in write mode.

Step 2: Here we have created our empty list records. In this list we will append lists containing details of records of students.

Step A: This is our while loop() which will keep on executing till our condition remains true. We have initialized a variable x with value 21 and used this variable to enter to our while loop. After entering to this loop user can exit to this loop only if he choose ‘n’ as choice it may exit to loop.

Step 3: After entering to while loop() we are taking students details as input that we want to store in our records.

Step 4: Here we have created new list data and initialized all taken user inputs to that list.

Step 5: Here now at step 5, we appended the list data to our previously defined empty list records. see at step 2. After that it will ask user as a choice that do they want to enter more data. If user chooses ‘n’ then they will exit to loop else loop keep on executing and keep on asking user to enter records.

Step 6: Here we have used dump() function to write all data to our binary file.

Important: It is very important to close() file after performing operation else the operations performed on file will not commit.

Also the data written to binary file is not in human readable format so to read that data we used load() function.