How to read binary file in Python

To read a binary file in Python, use the pickle.dump() method. The pickle.dump() method stores the object data in the file. To work with files in Python, use the file handling functions.

File handling plays a vital role in storing the data permanently into a file at a specific location so that we can access the data or the file later, even after the program terminates.

In this article, we will mainly concentrate on binary files. But before moving ahead, let us get familiarized with the types of files that we can play within Python.

Types of files in Python

Text files

Text files are those files in which data is stored in ASCII (American Standard Code for Information Interchange) code, i.e., a file containing numbers, letters, and symbols.

CSV files (Comma Separated Values)

CSV files are also text files in which data is stored in a specific format that allows us to save the data in rows and columns format.

Binary files

Binary files are those types of files in which data is stored in the format of zeros and ones, i.e., “0” & “1”. Compared to other files, and Binary files are faster because the computer is programmed to understand the 0’s and 1’s language.

Binary Files in Python

To work with binary files in Python, import the PICKLE module. So whenever we deal with binary files, you have to import the PICKLE module.

There are two essential terms to keep in mind that is,

Pickling

Pickling is the process where a Python Structure(list/dictionary) or Python object is converted to zeros (‘0’) and ones (‘1’) format, i.e., the byte format.

Unpickling

Unpickling is the process of converting a binary file to a Python Structure(list/dictionary) or Python object. And hence, we can say that pickling is just the opposite of unpickling.

As this article is related to “How to read binary files in Python”, our main area of interest is related to “Unpickling,” as we have to read the binary file. So if we want to write something in the binary file, our main area of interest would be “Pickling”.

The pickle module gives us the advantages of two functions, which are,

Python pickle.dump()

The pickle.dump() function writes the content/data into the binary files.

Syntax

pickle.dump(structure, file_object)

Python pickle.load()

The pickle.load() function reads the content/data from the binary files.

Syntax

Structure(List/dictionary) = pickle.load(file_object)

Since we have to read the content from the binary file, we will use the load() function. But, first, let us take an example program to understand the load() function.

import pickle

f = open("myFile.dat", "rb")

list1 = pickle.load(f)

print(list1)

f.close()

Output

['Dipam', 'Pratiksha', 'Priyanka']

Conclusion

To read from a binary file, use the Unpickling process in which you need to use a pickle.load() function. To write into a binary file, we have to use pickling by importing the pickle module.

In short, we can say that,

Python object/ Structure               ————🡪 Byte stream (“0’s” and “1’s”)        [PICKLING]

Byte stream (“0’s” and “1’s”)        ————🡪 Python object/ Structure               [UNPICKLING]

To read from a binary file, we need to import the PICKLE module and, from that module, use the load() function.

That’s it for this tutorial.

See also

How to find all files with extensions in Python

How to write json file in Python

How to make requirements.txt file in Python

Leave a Comment