1

I am writing a program to read and write bits from a file to another file. I found a library called bitstring that helps to manipulate bits as strings. However, this library helps me to read bits, but I cannot write the read bits. Both inputs and outputs files have the same size, so it will be no problem in term of bytes. This is a part of my code.

import bitstring


file = bitstring.ConstBitStream(filename='paper.pdf')
print(file.length)

bits_to_read = 5000000
last_bits = 0

while file.pos < file.length-bits_to_read:
    bits = file.read(bits_to_read)
    str_bits = bitstring.BitArray(bits).bin



rest = file.length - file.pos
bits = file.read(rest)
str_bits = bitstring.BitArray(bits).bin

with kind of regards.

  • 1
    What do you mean *"I cannot write the read bits"*. Show us which functions you tried, and what the result was? Did you open the second file in append or append-binary mode? – smci May 06 '21 at 22:27
  • So, I'm reading bits from a file and do on them treatments, encryption. Next, I want to write the resulted bits into another file. The only solution I have is to write as bytes. but, I wanna do it like streaming bits. – Mounir Bouhamed May 08 '21 at 09:34
  • There are 32 hits on [\[python\] write bitstring](https://stackoverflow.com/search?q=%5Bpython%5D+write+bitstring), can you explain (in the question body, not here in comments) why what you want to do is different? Also, on SO you have to show what code you attempted, and any errors it gives. – smci May 08 '21 at 09:51
  • @smci thx, just found a solution. – Mounir Bouhamed May 08 '21 at 11:16
  • Does this answer your question? [Write boolean string to binary file?](https://stackoverflow.com/questions/12672165/write-boolean-string-to-binary-file) – smci May 08 '21 at 21:16
  • I'm pretty sure this is a duplicate. Please please check the existing Q&A and confirm which it's a duplicate of. That helps improve SO. – smci May 08 '21 at 21:17
  • I checked before, it is not the same solution. I checked those solutions aren't fit my needs. So, I hope this new solution will help others. – Mounir Bouhamed May 11 '21 at 18:16
  • Ok can you please explain in words (above, edit in the question body, not down here in comments) exactly why it's not a duplicate? with links to some related questions? Because it still looks like a duplicate to me and presumably millions of others. Help us understand why it isn't. – smci May 11 '21 at 23:21

1 Answers1

1

So, I have found a solution. I appended the resulted bits into one variable and next, I exported. This is a part of the code:

while file.pos < file.length-bits_to_read:
    bits = file.read(bits_to_read)
    str_bits = bitstring.BitArray(bits).bin
    encrypted_bits = ''.join(encrypt(str_bits, cipher))
    exported_str = exported_str + encrypted_bits

rest = file.length - file.pos
bits = file.read(rest)
str_bits = bitstring.BitArray(bits).bin
exported_str = exported_str + str_bits

exported_bits = bitstring.BitArray(bin=exported_str)
with open(output_name, 'wb') as f:
    f.write(exported_bits.tobytes())
LaytonGB
  • 1,384
  • 1
  • 6
  • 20