So I have a stream of bits in python that looks like that:
bitStream = "001011000011011111000011100111000101001111100011001"
Now this stream is dynamic, meaning that it changes depending on the input received, now I want to write this to a file in python, I'm currently doing that:
f = open("file.txt", "rb+")
s = file.read() # stream
bitStream = "001011000011011111000011100111000101001111100011001"
byteStream = int(bitStream,2).to_bytes(len(bitStream)//8, 'little')
f.close() #close handle
However that works but the thing is that the bit stream can be a non-8bits aligned string which results in a file write of n-1 bytes or an error of the type int too big to convert.
Now normally I would align the file bits to be divisible by 8 (which is normal behavior) but in this case I really cannot add bits because otherwise, when I would give again this file to my program it will misinterpret the alignment bits as something other than expected.
Would you guys have any idea?
Thanks in advance