Reading Multiple Data 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.

Very Important: If we are reading record of one student (roll no, name, class, section etc.) in binary file, then we all know that we will get a list, tuple, dictionary etc. storing the details of one student.

For storing records of multiple student we used the concept of nested list in our previous topic. Hence, if we read records of all students than we will get the output as nested list ie.

file: “Abc.dat”


We are storing the records of two student (roll no, name, class, section etc.) in our Binary File. This is not the perfect way to get the result. We will try to get the result in perfect readable format.


Problem Solving Approach: We are getting nested loop as a output when we have stored multiple records in our binary file. So, here our approach is to get each record of student to display in each line for ease of reading. We have nested loop, so here we can use for loop along with load() method to get each record displayed.

Example 1:
I have shown you both code for Reading Data From Binary File. We have defined two functions first one is read() and second one is read2().

  • read(): In this function we used normal way for reading data from Binary File as we have did before in our previous topics. How to read data from Binary File in Python ?
  • read2(): We used for loop along with load() function to get each list from nested list to display records one by one in each line.