Reading Binary Files in Python
An Introduction
Using Python to interact with non-textual data such as photos, audio, and other types of data requires the ability to read binary files. A binary file can be opened for reading using the built-in open() function with the 'rb' mode. Using methods like read(), readline(), or readlines() after the file has been opened, you can read its contents. When reading binary data, you will see a series of bytes you can process or modify as necessary. Binary data must be handled carefully because encoding or decoding can corrupt data. Always close() to close the file, or use the "with" statement for better resource management.
Reading the String Data from the Binary File into the Bytes Array
The binary file can be read in Python in numerous ways. You can read a specific number of bytes or the entire binary file simultaneously. Use the script below to generate a Python file. The string.bin file was opened for reading using the open() function. In each iteration of the while loop and print, the read() method has retrieved seven characters from the file. The complete contents of a binary file that will be printed later have been read using the read() function, which has been used in the following step without any arguments.
Example
file_handler = open("string.bin", "rb")
data_byte = file_handler.read(7)
print("Print three characters in each iteration:")
while data_byte:
print(data_byte)
data_byte = file_handler.read(7)
with open('string.bin', 'rb') as fh:
content = fh.read()
print("Print the full content of the binary file:")
print(content)
Output
Print three characters in each iteration:
b'Welcome'
b' to pyt'
b'hon pro'
b'grammin'
b'g langu'
b'age'
Print the full content of the binary file:
b'Welcome to python programming language'
Explanation
This Python program double-clicks the "string.bin" binary file. It first reads and outputs the first seven bytes of data in an extended loop, but it doesn't correctly loop for additional readings. The entire file's content is then accurately read using a "with" statement and saved in the "content" variable. The code's goal is to show binary file reading. Still, depending on the situation, it should also have a looping for continuous reading and consider additional binary data processing.
Reading an Array Data from the Binary File of String Data
Write a Python file using the following Python script to access a binary file previously created with the name number_list.bin. Numerous numerical data are listed in this binary file. The open() function uses the binary file to be opened for script reading, the same as in the previous example.
Next, a list will be created from the first five integers after they have been read from the binary file, and then they will be printed.
Example
file = open("string.bin", "rb")
number = list(file.read(5))
print(number)
file.close()
Output
[87, 101, 108, 99, 111]
Explanation
This Python script launches the read binary mode ('rb') while opening the binary file "string.bin". It extracts the first five bytes from the file, breaks them down to a list of distinct bytes, and keeps the list in the variable "number." After that, the list of bytes is printed. The code finally shuts the file using the 'file to guarantee optimal resource management. Close ()' method. This code shows how to read and store a list of bytes representing a chunk of binary data from a file.
Reading Binary Files using NumPy
This section of the tutorial has demonstrated how to generate a binary file using a NumPy array and how to read the content of a binary file into a list using a NumPy module. Before running the script below, you must install the NumPy module. You can do this by running the following command in the terminal or by downloading the NumPy package in the Python editor, which is where the script will be run. The fromfile() function creates an array by receiving a text or binary file, whereas the tofile() function creates a text or binary file.
Example
import numpy as np
file_path = "string.bin"
data_type = np.float32 # Change this to the appropriate data type
try:
with open(file_path, "rb") as file:
binary_data = np.fromfile(file, dtype=data_type)
print("Binary data:")
print(binary_data)
except FileNotFoundError:
print(f"File '{file_path}' not found.")
except Exception as e:
print(f"An error occurred: {str(e)}")
Output
Binary data:
[4.3607339e+21 1.9433263e-19 1.9860936e+29 7.1941329e+28 4.7561989e+30
2.7947283e+20 1.8060574e+28 2.7223540e+20 2.8293157e+20]
Conclusion
This class has demonstrated three distinct approaches to reading the binary file using straightforward examples. The contents of the binary file were returned in the first example as a byte array. The binary file's content was produced in the second case as a list. The binary file's content was also produced in the previous example as a list.