Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\\exportTest1.zip",'rb' ) as file:
    zipContents = file.read()
    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\exportTest1.zip"
$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.

like image 607
Mario Majcica Avatar asked Jun 21 '16 12:06

Mario Majcica


People also ask

How do I encode base64 in Python 3?

Encoding Strings with Python Python 3 provides a base64 module that allows us to easily encode and decode information. We first convert the string into a bytes-like object. Once converted, we can use the base64 module to encode it.

How do I decode base64 text file in Python?

To decode an image using Python, we simply use the base64. b64decode(s) function. Python mentions the following regarding this function: Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.

What is base64 encoding in Python?

The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system.


2 Answers

From the base64 package doc:

base64.encodestring:

"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

base64.b64encode:

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

Example:

import base64

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

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

like image 132
Simon Kirsten Avatar answered Sep 20 '22 14:09

Simon Kirsten


Use b64encode to encode without the newlines and then decode the resulting binary string with .decode('ascii') to get a normal string.

encodedZip = base64.b64encode(zipContents).decode('ascii')
like image 36
gre_gor Avatar answered Sep 17 '22 14:09

gre_gor