Python Forum
How do I read and write a binary file in Python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I read and write a binary file in Python? (/thread-40076.html)



How do I read and write a binary file in Python? - blackears - May-29-2023

I need to read and write a binary file. This file has a variety of floats, short ints, single bytes and strings in it. Most programming languages provide you with a way to read various data types from a stream, but I cannot find anything like that is Python. Is there any way to open a file to be read for binary and then issue a set of commands like "read a float", "read a byte", "read an unsigned int", "read a utf string n bytes long"?
def read_some_data(context, filepath):
    with open(filepath, 'rb') as source_file:
        header = source_file.read(12)
        #read float
        #read ushort
        ????



RE: How do I read and write a binary file in Python? - menator01 - May-29-2023

duckduckgo turned up these searches
https://docs.python.org/3/library/io.html
https://stackoverflow.com/questions/4290716/how-to-write-bytes-to-a-file-in-python-3-without-knowing-the-encoding
https://www.delftstack.com/howto/python/write-bytes-to-file-python/


RE: How do I read and write a binary file in Python? - blackears - May-29-2023

Those are great if you already have a buffer to write, but how do you construct the buffer in the first place? Does Python have streams? Is there a way to just pull the next float off of a stream?


RE: How do I read and write a binary file in Python? - deanhystad - May-30-2023

Python does not have unsigned types. It has bytes, but no shorts or longs. Those are C types. Python types are unsized.

If you need to work with C-type values you can use the ctypes library.

https://docs.python.org/3/library/ctypes.html

Or you can use the struct library.

https://docs.python.org/3/library/struct.html

I think the struct library sounds like a better fit for what you describe.


RE: How do I read and write a binary file in Python? - Gribouillis - May-30-2023

You could perhaps look into this scanf module. Sorry, I don't think it handles a binary stream finally.

As @deanhystad wrote above, the struct module can be used

def read_float(f):
    s = f.read(4)
    return struct.unpack('f', s)

def read_ushort(f):
    s = f.read(2)
    return struct.unpack('H', s)

def read_utf8(f, n):
    s = f.read(n)
    return s.decode()



RE: How do I read and write a binary file in Python? - blackears - May-30-2023

The struct package looks like what I need. I've used it to write a simple stream reader which is doing the job I need.


RE: How do I read and write a binary file in Python? - rajeshgk - Jun-06-2023

Python provides several built-in modules that allow you to read and write binary files with various data types. The struct module is commonly used for this purpose. It allows you to pack and unpack binary data according to specified format strings.

Here's an example that demonstrates how to read different data types from a binary file using the struct module:

import struct

# Read binary file
with open('data.bin', 'rb') as file:
# Read a float (4 bytes)
float_value = struct.unpack('f', file.read(4))[0]
print(f"Float: {float_value}")

# Read a byte
byte_value = struct.unpack('B', file.read(1))[0]
print(f"Byte: {byte_value}")

# Read an unsigned int (2 bytes)
uint_value = struct.unpack('H', file.read(2))[0]
print(f"Unsigned Int: {uint_value}")

# Read a UTF string (n bytes long)
string_length = struct.unpack('B', file.read(1))[0]
string_value = file.read(string_length).decode('utf-8')
print(f"String: {string_value}")

In this example, data.bin is the binary file you want to read. The open() function is used to open the file in binary mode ('rb'). Then, you can use struct.unpack() to read specific data types from the file based on the format string.

The format string specifies the byte order, size, and type of the data you want to read. For example, 'f' represents a 4-byte float, 'B' represents a single byte, and 'H' represents a 2-byte unsigned int. You can refer to the struct module documentation for more information on format string options.

Note that you need to ensure the format strings match the actual data layout in the binary file. If the format strings are incorrect, it may lead to reading incorrect values or throwing exceptions.

Remember to adjust the file path and format string according to your specific binary file structur