A robot log reader can receive every byte of a file and still lose records if it treats each read as a record boundary. A fixed-width format defines where records begin and end. A file reader may return chunks that stop between two fields or even inside one field. To decode the stream correctly, carry any bytes that do not yet form a complete record to the next read.

This example uses an invented sequence number and a signed sample value. The fixture ran in Python 3.14.7. It describes a local log export with no file header and no footer. It does not describe the wire format of a specific device.

Declare the six-byte record

Store the sequence number as a four-byte unsigned integer and the sample as a two-byte signed integer. Use little-endian byte order and standard field sizes by giving the Python struct format the prefix <. The explicit prefix controls byte order, sizes, and alignment. If a producer uses a different record layout, use the one specified for that file. Matching the expected record length alone does not establish that the values have been read correctly.

from struct import Struct

record = Struct("<Ih")

assert record.size == 6

payload = record.pack(7, -12) + record.pack(8, 34)

assert len(payload) == 12

assert list(record.iter_unpack(payload)) == [(7, -12), (8, 34)]

The first four bytes of the first record are the sequence number 7 in little-endian order. Using the big-endian form with the same field sizes interprets those bytes as 117440512. The signed sample also changes, from -12 to -2817. Both interpretations can yield an integer tuple, so a successful call is a structural check rather than a check of byte order or field meaning. Retain test fixtures with obvious signed values and sequence numbers.

Use the largest complete prefix of each buffer

In the test, the first read returns five bytes. The second returns four and the third returns three. The first chunk has no complete record. When those five bytes are combined with the next four, the buffer contains one complete record and three extra bytes. Carry the three extra bytes forward. Combining them with the final three yields the second record.

Python's iter_unpack requires a buffer whose length is a multiple of the record size. Supply just the complete prefix and keep the rest for the next read. Calling it on the initial five-byte chunk produces struct.error in this fixture.

def decode_records(chunks):

tail = b""

for chunk in chunks:

block = tail + chunk

usable = len(block) // record.size * record.size

yield from record.iter_unpack(block[:usable])

tail = block[usable:]

if tail:

raise ValueError("Incomplete final robot record")

chunks = [payload[:5], payload[5:9], payload[9:]]

assert list(decode_records(chunks)) == [(7, -12), (8, 34)]

The retained tail is at most five bytes for this format. Short reads are ordinary input to this function. A genuine end of stream is special because no more bytes can complete the tail. The Python I/O documentation describes reads that return fewer bytes than requested before end of file. A blocking file loop should continue after any nonempty read and stop on an empty result from a read with a positive size. A nonblocking reader has a separate distinction between data that is temporarily unavailable and an ended stream. Interpret those results before handing chunks to the decoder.

For a blocking file, a callable iterator can produce the byte chunks while the file remains open. The sentinel ends iteration when read returns an empty bytes object. The read size can be chosen independently of the record width. In a production import, use a size that balances read overhead with the amount of data buffered in memory.

with open("example.bin", "rb") as stream:

rows = list(decode_records(iter(lambda: stream.read(4096), b"")))

Handle partial output at end of file

The generator can yield complete records before it discovers a truncated final record. In the fixture, dropping the final byte causes an error at exhaustion, even though the first record can already have been yielded. If an import marks a file complete only after every record is valid, stage its outputs until the generator finishes successfully. A job that is allowed to retain a valid prefix should record that the file was incomplete and record the offset through the last confirmed record. A retry then has a position at which it can reconcile previous output.

The decoder assumes the first byte is aligned with the beginning of a record. If a format has a header or version marker, validate it and start at the documented payload offset. An extra byte inside the payload can shift how the remaining values are interpreted. Checking record size cannot identify every corruption or even detect the loss of an entire record. Where available, check the file's declared record count, checksum, sequence rules, and permitted field ranges. These checks answer different questions than the length of the final tail.

Test the read boundaries independently of the bytes

Use the same known payload and vary how it is divided into chunks. This example checked every two-part split from offset zero through offset twelve and also used the five, four, three split. The output stayed the same. Include an empty stream to exercise a normal zero-record result. Truncate the last record to exercise the failure. For a format with a known count, also remove a complete record and verify that the count check catches it. Keeping the bytes fixed while changing the read boundaries tests whether record assembly follows the format rather than the buffering behavior of one particular machine.

Sources checked November 28, 2026

Python 3.14 documentation, struct

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

Python 3.14 documentation, io

https://docs.python.org/3.14/library/io.html

Illustration. AI-generated concept showing a read boundary inside a fixed-width record on a robot diagnostic display.