2

I have a binary file with a small header, then 100 blocks of binary data in an identical format. I can read it like this:

with open(filename, "rb") as f:
    read_header(f)
    while 1:
        data = f.read(size)
        if len(data) != size:
            break
        do_stuff(data)

while 1/break seems weird for something that should be simple. Is there a way to write this as a for loop (without just hiding the while loop in a pointless extra class, or reading the entire file at once)?

Something like:

with open(filename, "rb") as f:
    read_header(f)
    for data in f.split(size):
        do_stuff(data)
Brendan Long
  • 53,280
  • 21
  • 146
  • 188

0 Answers0