Mario Majcica Mario Majcica - 1 year ago 248
Python Question

Python 3 and base64 encoding of a binary file

I'm new to Python and I do have an issue that is bothering me.

I use the following code to get a base64 string representation of my zip file.

with open( "C:\\Users\\Mario\\Downloads\\",'rb' ) as file:
zipContents =
encodedZip = base64.encodestring(zipContents)

Now, if I output the string it is contained inside a b'' representation. This for me is not necessary and I would like to avoid it. Also it adds a newlines character every 76 characters which is another issue. Is there a way to get the binary content and represent it without the newline characters and trailing and leading b''?

Just for comparison, if I do the following in PowerShell:

$fileName = "C:\Users\Mario\Downloads\"
$fileContentBytes = [System.IO.File]::ReadAllBytes($fileName)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)

I do get the exact string I'm looking for, no b'' and no \n every 76 chars.

Answer Source

From the base64 package doc:


"Encode the bytes-like object s, which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines (b"\n") inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME)."

You want to use


"Encode the bytes-like object s using Base64 and return the encoded bytes."


import base64

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

The decode() will convert the binary string to text.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download