Python Forum
How do I read and write a binary file in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I read and write a binary file in Python?
#1
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
        ????
Reply
#2
duckduckgo turned up these searches
https://docs.python.org/3/library/io.html
https://stackoverflow.com/questions/4290...e-encoding
https://www.delftstack.com/howto/python/...le-python/
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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?
Reply
#4
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.
Reply
#5
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()
Reply
#6
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.
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 232 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 449 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  How Write Part of a Binary Array? Assembler 1 357 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Recommended way to read/create PDF file? Winfried 3 2,902 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,552 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,475 Nov-09-2023, 10:56 AM
Last Post: mg24
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 604 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
Question Special Characters read-write Prisonfeed 1 634 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  read file txt on my pc to telegram bot api Tupa 0 1,135 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,127 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020