Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read complete file with bitstring

I want to read as many 24 bit chunks as possible from a file. How can I do this using bitstrings' ConstBitStream when I don't now how many chunks there are?

Currently I do this:

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

(here I have to calculate the number of events beforehand)

like image 585
HWende Avatar asked Jun 05 '12 08:06

HWende


2 Answers

You could read until an ReadError exeption is generated

try:
    while True:
        packet = eventList.read(24)
except ReadError:
    pass
like image 115
waynix Avatar answered Oct 16 '22 19:10

waynix


Catching the ReadError is a perfectly good answer, but another way is to instead use the cut method, which returns a generator for bitstrings of a given length, so just

for packet in eventList.cut(24):

should work.

like image 25
Scott Griffiths Avatar answered Oct 16 '22 19:10

Scott Griffiths