Convert zip file to bytes in python

Viewed 32

Goal for question - open a zip file and convert it into a bytes-like object.

When I tried I get the error:

encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'list'

Here is the code:

import base64

with open("results.zip", 'rb') as f:
    data = f.readlines()
    print(data)
    encoded = base64.b64encode(data)

I also tried this and got the same exact error:

import zipfile

with open("results.zip", 'rb') as f:
    data = f.readlines()
    zf = zipfile.ZipFile(io.BytesIO(data), "r")
    for fileinfo in zf.infolist():
        print(zf.read(fileinfo).decode('ascii'))
1 Answers

Thanks to @Vlad for his comment as it helped me to get the answer.

import base64

with open("results.zip", 'rb') as f:
    data = f.read()
    print(data)
    encoded = base64.b64encode(data)
Related