Question

I can read the first byte of a binary file like this:

with open(my_binary_file,'rb') as f:
    f.read(1)

But how do I do this with the module fileinput? If I run this code:

import fileinput
with fileinput.FileInput(my_binary_file,'rb') as f:
    f.read(1)

then I get this error:

AttributeError: 'FileInput' object has no attribute 'read'

Is there a module similar to fileinput, which allows me to read bytes/characters of multiple binary files instead of lines?

EDIT: Reading a line of the binary file and looping over it is not an option, as the binary file is large and contains no line breaks.

Was it helpful?

Solution

This is not the solution I was after, but this is the solution I ended up with:

def process_binary_files(list_of_binary_files):

    for file in list_of_binary_files:
        with open(file,'rb') as f:
            yield f.read(1)

    return

list_of_binary_files = ['f1', 'f2']
generate_byte = process_binary_files(list_of_binary_files)
byte = next(generate_byte)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top