codyc4321
codyc4321

Reputation: 9682

AttributeError: 'bytes' object has no attribute 'encode'; base64 encode a pdf file

I am trying to base64 encode a pdf in python. Several SO answers to this worked for other people but not on my end for some reason. My most recent attempt is:

# http://stackoverflow.com/questions/12020885/python-converting-file-to-base64-encoding
with open('/home/cchilders/projects/myproject/data/books/software-and-mind.pdf', 'rb') as f:
    encoded = f.read().encode("base64")
    print(encoded)

I get

AttributeError: 'bytes' object has no attribute 'encode'

How can I base64 this pdf file? Thank you

Upvotes: 5

Views: 27821

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113988

you should use the base64 module for this

import base64
base64.b64encode(f.read())

Upvotes: 9

Related Questions