Create a binary file in Python

How to create a small binary file in Python.

python


import struct

def main():    
    with open("data", "wb") as fout:        
        for x in [0xFF, 0x12, 0x89]:            
            fout.write(struct.pack("B", x))

if __name__ == "__main__":    
    main()

Let's check the contents of the file created by the hexdump command.

% hexdump data
0000000 ff 12 89
0000003

You can make it without any problems.

(Addition 2013-09-07)

shiracamus told me about bytearray in the comments. This is easier because you don't need to import.

python


def main():
    with open("data", "wb") as fout:
        bary = bytearray([0xFF, 0x12, 0x89])
        bary.append(0)
        bary.extend([1, 127])
        fout.write(bary)

if __name__ == "__main__":
    main()

This is the execution result of hexdump.

% hexdump data
0000000 ff 12 89 00 01 7f
0000006

Recommended Posts

Create a binary file in Python
[GPS] Create a kml file in Python
Create a function in Python
Create a GIF file using Pillow in Python
How to create a JSON file in Python
Create a MIDI file in Python using pretty_midi
Write a binary search in Python
Save the binary file in Python
Create a DI Container in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Create a deb file from a python package
File operations in Python
Create a Python module
Create SpatiaLite in Python
Binary search in Python
File operations in Python
Create a Python environment
Binary search in Python (binary search)
Create a Vim + Python test environment in 1 minute
Create an executable file in a scripting language
I want to create a window in Python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a Photoshop format file (.psd) with python
Create a package containing global commands in Python
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a shortcut to run a Python file in VScode on your terminal
Test & Debug Tips: Create a file of the specified size in Python
Take a screenshot in Python
Create a Wox plugin (Python)
Download the file in Python
Create gif video in Python
Binary search in Python / C ++
Algorithm in Python (binary search)
Make a bookmarklet in Python
Create a python numpy array
Create a dummy data file
Draw a heart in Python
Create a directory with python
Parse a JSON string written to a file in Python
Create a data collection bot in Python using Selenium
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
I want to randomly sample a file in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create your first GDSII file in Python using gdspy
Python script to create a JSON file from a CSV file
Run a Python file with relative import in PyCharm
[Python] Create a Tkinter program distribution file with cx_Freeze
Output a binary dump in binary and revert to a binary file
Create a fake Minecraft server in Python with Quarry
Create a 2d CAD file ".dxf" with python [ezdxf]
Create a Python image in Django without a dummy image file and test the image upload
Maybe in a python (original title: Maybe in Python)