To write bytes to a file in Python, use a with open() function and write in binary mode. The open() is a built-in Python function that returns it as a file object.
Regular users cannot understand the content present inside these files. Therefore, we have to convert them to strings to understand them. To read a binary file in Python, use the pickle.dump() function.
Python Program to write bytes to file
data = b'\x22\x48\x49\x49\x22' with open("sample.txt", "wb") as file: file.write(data)
It will create a sample.txt file inside your current working directory with the following content.
In this program, we created a variable called data. This is stored with some bytes prefixed with b. The b denotes a string as binary data. Then, we opened a file called sample.txt in write with binary mode. This file is aliased as a file.
In the next line, we wrote the contents from the variable to a file using the function called write(). The file.write() is a built-in Python function that writes all the information from the bytes variable to the file. But the contents stored in the file will be stored as strings.
Since we wrote the binary data to the file, it converts the binary to texts, and we can read the text in the file.
data = b'\x22\x48\x49\x49\x22' with open("sample.txt", "wb") as file: file.write(data) with open("sample.txt", 'r') as file: content = file.read() print(content)
In this program, we read the contents from the sample.txt file. This reads all the contents from the file and stores it in a variable called content. Then, we printed the content variable. If we execute this program, we can see the output as:
The values in the data variable are the ASCII values of “HII”. Hence the write function writes the data as “HII” to the file.
Write bytes to file using bytearray() function
The bytearray() is a built-in Python method that returns a bytearray object, an array of the given bytes. You can write bytes to a file using the bytearray() function.
arr = [72, 69, 76, 76, 79] data = bytearray(arr) content = bytes(data) with open("sample.txt", "wb") as file: file.write(content) with open("sample.txt", "r") as file: content = file.read() print(content)
In this program, we created a list consisting of some numbers. Next, we created a bytearray from the list using the bytearray() function. The bytearray() function converts the array to the bytearray. Then we converted the bytearray into bytes using the bytes(). The bytearray() function converts the numbers into their ASCII values.
Each number in the list is equivalent to an ASCII character, and that’s why the bytearray() function converts the number to their ascii values. Then, the sample.txt file is opened in the write binary mode.
This content is written to the file using a write function. Then, we read the contents of the file using the read function. We have displayed the contents present in the file. The output of the program looks like this:
The output is the equivalent ASCII characters for the numbers in the list.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.