pythonbytebinascii

Writing a binary file with lines more than 45 bytes: binascii.Error: At most 45 bytes at once


My goal is to read a binary file and convert it to text. My code is :

def binary_to_text(self,file_name):
  open_file = open(file_name,"rb")
  with open("Binary2Text.txt", "a") as the_file:
    for line in open_file:
      the_file.write(binascii.b2a_uu(line))

I'm getting this error:

binascii.Error: At most 45 bytes at once

Is there a way to get around this, or is there another module I can use besides binascii? Thanks!


Solution

  • The binascii.b2a_uu method is meant to be a low-level function for performing uuencode, in which the algorithm encodes text input in 45-byte chunks, which is why there is a 45-byte chunk limit for input.

    Unless you're trying to implement uuencode yourself, you should simply use the uu.encode method instead:

    import uu    
    def binary_to_text(self, file_name):
        with open("Binary2Text.txt", "a") as the_file:
            the_file.write(uu.encode(file_name))