How to Read Binary Files in Python

The file that contains the binary data is called a binary file. Any formatted or unformatted binary data is stored in a binary file, and this file is not human-readable and is used by the computer directly.  When a binary file is required to read or transfer from one location to another location, the file’s content is converted or encoded into a human-readable format. The extension of the binary file is .bin. The content of the binary file can be read by using a built-in function or module. Different ways to read binary files in Python have been shown in this tutorial.

Pre-requisite:

Before checking the examples of this tutorial, it is better to create one or more binary files to use in the example script. The script of two python files has given below to create two binary files. The binary1.py will create a binary file named string.bin that will contain string data, and the binary2.py will create a binary file named number_list.bin that will contain a list of numeric data.

Binary1.py

# Open a file handler to create a binary file

file_handler = open("string.bin", "wb")

# Add two lines of text in the binary file

file_handler.write(b"Welcome to LinuxHint.\nLearn Python Programming.")

# Close the file handler

file_handler.close()

Binary2.py

# Open a file handler to create a binary file

file=open("number_list.bin","wb")

# Declare a list of numeric values

numbers=[10,30,45,60,70,85,99]

# Convert the list to array

barray=bytearray(numbers)

# Write array into the file

file.write(barray)

file.close()

Example-1: Read the binary file of string data into the byte array

Many ways exist in Python to read the binary file. You can read the particular number of bytes or the full content of the binary file at a time. Create a python file with the following script. The open() function has used to open the string.bin for reading. The read() function has been used to read 7 characters from the file in each iteration of while loop and print. Next, the read() function has been used without any argument to read the full content of the binary file that will be printed later.

# Open the binary file for reading

file_handler = open("string.bin", "rb")

# Read the first three bytes from the binary file

data_byte = file_handler.read(7)

print("Print three characters in each iteration:")

# Iterate the loop to read the remaining part of the file

while data_byte:

    print(data_byte)

    data_byte = file_handler.read(7)


# Read the entire file as a single byte string

with open('string.bin', 'rb') as fh:

    content = fh.read()

print("Print the full content of the binary file:")

print(content)

Output:

The following output will appear after executing the above script.

Example-2: Read the binary file of string data into the array

Create a python file with the following script to read a binary file named number_list.bin created previously. This binary file contains a list of numeric data. Like the previous example, the open() function has used open the binary file for reading in the script. Next, the first 5 numbers will be read from the binary file and converted into a list before printing.

# Open the binary file for reading

file = open("number_list.bin", "rb")

# Read the first five numbers into a list

number = list(file.read(5))

# Print the list

print(number)

# Close the file

file.close()

Output:

The following output will appear after executing the above script. The binary file contains 7 numbers, and the first five numbers have printed in the output.

Example-3: Read binary file using NumPy

The ways to create the binary file using the NumPy array and read the content of the binary file using into a list by using the NumPy module have shown in this part of the tutorial. Before checking the script given below, you have to install the NumPy module by executing the command from the terminal or installing the NumPy package in the Python editor, where the script will be executed. The tofile() function is used to create a text or binary file, and the fromfile() function is used to create an array by reading a text or binary file.

Syntax of tofile():

ndarray.tofile(file, sep='', format='%s')

The first argument is mandatory and takes the filename or string or path as a value. The file will be created if a filename is provided in this argument. The second argument is optional that is used to separate the array elements. The third argument is optional also and used for formatting the output of the text file.

Syntax of fomfile():

numpy.fromfile(file, dtype=float, count=- 1, sep='', offset=0, *, like=None)

The first argument is mandatory and takes the filename or string or path as a value. The content of the file will be read if a filename will be provided in this argument. The dtype defines the data type of the returned array. The count is used to count the number of items. The purpose of the sep is to separate the text or array items. The offset is used to define the current position of the file. The last argument is used to create an array object that not a NumPy array.

Create a python file with the following script to create a binary file using NumPy array and read and print the content of the binary file.

# Import NumPy module

import numpy as np

# Declare numpy array

nparray = np.array([34, 89, 30, 45, 90, 11])

# Create binary file from numpy array

nparray.tofile("list.bin")

# Print data from the binary file

print(np.fromfile("list.bin",  dtype=np.int64))

Output:

The following output will appear after executing the above script.

Conclusion:

Three different ways to read the binary file have been shown in this tutorial by using simple examples. The first example returned the content of the binary file as a byte array. The second example returned the content of the binary file as a list. The last example also returned the content of the binary file as a list.



from Linux Hint https://ift.tt/3yJirOc

Post a Comment

0 Comments