Write Data in Binary Mode in Python
In Python, you can write data in binary mode using the built-in open()
function with the mode set to 'wb'
. This mode allows writing binary data such as images, audio files, or any raw byte sequences directly to a file.
Examples to Write Binary Files
1. Writing Binary Data to a File
In this example, we will write a sequence of bytes to a binary file.
main.py
</>
Copy
# Open a file in binary write mode
with open("binary_file.bin", "wb") as file:
# Define binary data
binary_data = b'\x48\x65\x6C\x6C\x6F' # "Hello" in ASCII
# Write binary data to file
file.write(binary_data)
print("Binary data written successfully.")
Explanation:
open("binary_file.bin", "wb")
: Opens a file namedbinary_file.bin
in binary write mode.b'\x48\x65\x6C\x6C\x6F'
: A byte sequence representing the ASCII characters of “Hello”.file.write(binary_data)
: Writes the binary data to the file.
Output:

binary_file.bin content:

2. Writing Binary Data from a Bytes Object
We can also write binary data created from a bytes
object.
main.py
</>
Copy
# Open a file in binary write mode
with open("binary_file.bin", "wb") as file:
# Create a bytes object from a list of integers
numbers = bytes([65, 66, 67, 68, 69]) # Corresponds to "ABCDE"
# Write binary data to file
file.write(numbers)
print("Binary numbers written successfully.")
Explanation:
bytes([65, 66, 67, 68, 69])
: Converts a list of integers into a bytes object.file.write(numbers)
: Writes the byte sequence to the file.
Output:

binary_file.bin content:

3. Writing Binary Data from a String
We can convert a string into bytes using the encode()
method and write it in binary mode.
main.py
</>
Copy
# Open a file in binary write mode
with open("binary_file.bin", "wb") as file:
# Convert string to bytes
text = "Sample Data. Binary Mode Writing."
binary_text = text.encode('utf-8')
# Write binary data to file
file.write(binary_text)
print("String written in binary mode successfully.")
Explanation:
text.encode('utf-8')
: Converts the string into bytes using UTF-8 encoding.file.write(binary_text)
: Writes the encoded byte data to the file.
Output:

binary_file.bin content:

4. Writing Binary Data from an Image
We can copy an image file by reading and writing in binary mode.
main.py
</>
Copy
# Open the source image in binary read mode
with open("source_image.jpg", "rb") as source:
# Read binary content
image_data = source.read()
# Open a new file in binary write mode
with open("copied_image.jpg", "wb") as destination:
# Write the binary data
destination.write(image_data)
print("Image copied successfully in binary mode.")
Explanation:
open("source_image.jpg", "rb")
: Opens an image in binary read mode.image_data = source.read()
: Reads the binary data.open("copied_image.jpg", "wb")
: Opens a new image file in binary write mode.destination.write(image_data)
: Writes the copied binary content.
Output:
Image copied successfully in binary mode.
Conclusion
In this tutorial, we have covered various ways to write data in binary mode:
- Using
wb
mode inopen()
to write raw binary data. - Writing a
bytes
object directly to a file. - Encoding strings into binary format using
encode()
. - Reading and writing images in binary mode for file operations.