2016-03-25 33 views
3

我想base64編碼在Python中的PDF。幾個SO的答案爲其他人工作,但由於某種原因,我沒有結束。我最近一次嘗試是:AttributeError:'bytes'對象沒有'encode'屬性; 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) 

我得到

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

我怎樣才能BASE64這個pdf文件?謝謝

+1

'進口的base64; base64.b64encode(f.read())' –

+0

試圖與readlines方法。堅持下去,非常感謝 – codyc4321

+0

,它不喜歡'readlines',但'read'很好。如果你想分手,我接受 – codyc4321

回答

8

你應該使用的base64模塊此

import base64 
base64.b64encode(f.read()) 
相關問題