Hello and thanks for reading. I am running into some issues decoding previously encoded files using base64. For example, suppose I want to encode a pdf file using base64. The result is a nice 80 char delimited series of strings. The code that does the encoding (cribbed from this board) is nice and easy:
def encode_file_base64(bin_input):
flag = 0
try:
with open(bin_input, 'rb') as fin, open('tmp.bin_hex', 'w') as fout:
base64.encode(fin, fout)
except:
traceback.print_exc()
flag = -1
return flag
Now the decoding function:
def decode_file_base64(bin_output):
flag = 0
try:
with open('tmp.bin_hex', 'rb') as fin, open(bin_output, 'w') as fout:
base64.decode(fin, fout)
except:
traceback.print_exc()
flag = -1
return flag
It does the job, but when I try to open the output file, I am not able to and the file appears to be 'corrupt'. I have been struggling with this more than a fair amount and I'm about to give up. I suppose I could use other types of encodings but the BOSS insists on base64 (he must have heard that it's the best...).
-1is not helpful to the caller, where a propagated exception can allow the caller to log the error message.returnis also not pythonic; justreturn 0andreturn -1and get rid offlag. And the inconsistent indentation (anywhere from 1 to 4 characters in different places) is not pythonic, and seems to have led you to a bug that I didn't notice: thebase64.encodeis not indented under thewith, so you won't even be able to run this.