Python - Write Bytes to File
Files are used in order to store data permanently. File handling is performing various operations (read, write, delete, update, etc.) on these files. In Python, file handling process takes place in the following steps:
- Open file
- Perform operation
- Close file
There are four basic modes in which a file can be opened― read, write, append, and exclusive creations. In addition, Python allows you to specify two modes in which a file can be handled― binary and text. Binary mode is used for handling all kinds of non-text data like image files and executable files.
Examples
Example 1: Open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.
some_bytes = b'\xC3\xA9'
with open("my_file.txt", "wb") as binary_file:
binary_file.write(some_bytes)
Output:
Explanation: This code writes a byte sequence (some_bytes) to a file in binary mode. It opens my_file.txt in wb mode, and writes the bytes (b'\xC3\xA9') to it, representing the Unicode character é. The with statement ensures the file is properly closed after writing.
Example 2: This method requires you to perform error handling yourself, that is, ensure that the file is always closed, even if there is an error during writing. So, using the "with" statement is better in this regard as it will automatically close the file when the block ends.
some_bytes = b'\x21'
binary_file = open("my_file.txt", "wb")
binary_file.write(some_bytes)
binary_file.close()
Output:
Explanation: This code writes a byte sequence (some_bytes) to a file in binary mode. It opens my_file.txt in wb mode, writes the byte (b'\x21', representing the exclamation mark !), and then explicitly closes the file using binary_file.close().
Example 3: Also, some_bytes can be in the form of bytearray which is mutable, or bytes object which is immutable as shown below.
byte_arr = [65,66,67,68]
some_bytes = bytearray(byte_arr)
some_bytes.append(33)
immutable_bytes = bytes(some_bytes)
with open("my_file.txt", "wb") as binary_file:
binary_file.write(immutable_bytes)
Output:
Explanation: This code creates a byte array from a list of integers, appends a byte (33 representing !), and converts it to an immutable bytes object before writing it to a file. The file my_file.txt is opened in binary write mode (wb), and the bytes object is written to it. The with statement ensures the file is closed properly after writing.
Example 4: Using the BytesIO module to write bytes to File
from io import BytesIO
write_byte = BytesIO(b"\xc3\x80")
with open("test.bin", "wb") as f:
f.write(write_byte.getbuffer())
Output:

Explanation: This code uses BytesIO to create an in-memory binary stream (write_byte) containing the bytes (b"\xc3\x80"). It then writes the content of the in-memory stream to a file (test.bin) in binary write mode (wb). The getbuffer() method retrieves the binary data from the BytesIO object for writing. The with statement ensures the file is properly closed after the operation.