当我尝试这段代码时:
#!/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/