How to read a complete file with a bit string

I want to read as many as 24 bits of fragments from a file. How to do this using ConstBitStream bitstrons when I'm not right now, how many pieces are there?

I am currently doing this:

eventList = ConstBitStream(filename = 'events.dat')
for i in range(1000) :
    packet = eventList.read(24)

(here I have to calculate the number of events in advance)

+5
source share
2 answers

You can read until a ReadError is created.

try:
    while True:
        packet = eventList.read(24)
except ReadError:
    pass
+4
source

Capturing ReadErroris a great answer, but instead use the cut method , which returns a generator for bitrates of a given length, so just

for packet in eventList.cut(24):

must work.

+3
source

All Articles