我想使用函数base64.encode()直接用Python编码文件的内容。
文档指出:
input 64.编码(输入、输出) 对二进制输入文件的内容进行编码,并将生成的base64编码数据写入输出文件。输入和输出必须是文件对象。
所以我就这么做:
encode(open('existing_file.dat','r'), open('output.dat','w'))并得到以下错误:
>>> import base64
>>> base64.encode(open('existing_file.dat','r'), open('output.dat','w'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/base64.py", line 502, in encode
line = binascii.b2a_base64(s)
TypeError: a bytes-like object is required, not 'str'在我看来,这看起来像是/usr/lib/python3.6/base64.py中的一个bug,但我很大一部分人不想相信.
发布于 2017-06-19 11:22:53
来自文档
打开二进制文件时,应将
'b'附加到模式值中,以二进制模式打开该文件。
如此的改变
encode(open('existing_file.dat','r'), open('output.dat','w'))至
encode(open('existing_file.dat','rb'), open('output.dat','wb'))应起作用
https://stackoverflow.com/questions/44629159
复制相似问题