python - base64 类型错误,TypeError : expected bytes, 不是 str

标签 python python-3.x file-io base64 typeerror

当我尝试这段代码时:

#!/usr/bin/python3.2

import time, sys, os
import base64
#####################
tmp = "/home/gh.txt"
ex = "/home/ex.txt"
if (len(sys.argv) < 2):
    print("enter name of the new dict and key")
else:
    global ns
    ns = sys.argv[1]
    global ps
    ps = sys.argv[2]

dss = "{<%s>:%s}" % (ns, ps)
os.system("touch dss0")
fi0 = open(tmp, "a")

fi0.write(dss)
d64i = base64.b64encode(tmp)
fi = open(ex, "a")
fi.write(d64i)
os.system("rm tmp")

我得到了这个错误:

File "./n64.py", line 19, in <module>
     d64i=base64.b64encode("/home/gh.txt")
File "/usr/lib/python3.2/base64.py", line 56, in b64encode
     raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str

最佳答案

嗯,这就是它所说的。 base64.b64encode()bytes 作为参数,不是字符串

d64i = base64.b64encode(bytes(tmp, 'utf-8'))

关于python - base64 类型错误,TypeError : expected bytes, 不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187916/

相关文章:

python - 在 python numpy 中创建动态数组名称

python - GQL 内部交易

python - 打印斐波那契数列

python - Python 和 .NET 之间二进制文件的兼容性

mysql - LOAD DATA INFILE 字段终止于

android - 应用程序将文件写入内部 SD 卡,但 Windows 看不到该文件

PHP 和 Python 集成 : Calling python scripts from PHP page and respond back to it

Python 在字典上实现循环

python - Random.Randint() 重复

python - 在Python Plotly中是否有相当于直方图分箱的条形图?