Search code examples
pythonencodingbase64

Encoding large file(500MB+) to base64 in python


I am trying to base64 encode a large exe file which is around 500 MB and while doing it i am getting this error:

F:\python\waft>python count.py
Traceback (most recent call last):
  File "count.py", line 10, in <module>
    encodedZip = base64.b64encode(f.read())
  File "C:\Users\rohan-pc\AppData\Local\Programs\Python\Python38-32\lib\base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
MemoryError

This is the code i wrote to generate base64 from exe. I did some research and got to know that there are some memory limitation but is there any other way that i can encode larger files..?

with open("test.exe", "rb") as f:
    encodedZip = base64.b64encode(f.read())

Solution

  • After doing some research i got the answer the following code works :

    fr = open("test.exe", 'rb')
    
    i = 1
    while True:
        piece = fr.read(75232000)
        if not piece:
            break
        fw = open(str(i), 'wb')
        fw.write(base64.b64encode(piece))
        fw.close()
        i += 1
    fr.close()