Write Binary File

Create a small binary file from bytes.

PythonBeginner
Python
# Program to write a small binary file

filename = input("Enter binary filename to write: ")

data = bytes([0, 1, 2, 3, 255])

with open(filename, "wb") as f:
    f.write(data)

print("Binary data written to", filename)

Output

Enter binary filename to write: sample.bin
Binary data written to sample.bin

Opening in "wb" mode writes raw bytes; here we create a simple bytes object as a demo.