AttributeError:'bytes'对象没有属性'encode'; base64编码pdf文件

cod*_*321 4 python pdf post base64

我想在python中对base64编码pdf.这个问题的几个答案对其他人有用,但出于某种原因不在我身边.我最近的尝试是:

# 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)
Run Code Online (Sandbox Code Playgroud)

我明白了

AttributeError: 'bytes' object has no attribute 'encode'
Run Code Online (Sandbox Code Playgroud)

我该如何base64这个pdf文件?谢谢

Jor*_*ley 9

你应该使用base64模块

import base64
base64.b64encode(f.read())
Run Code Online (Sandbox Code Playgroud)