base64 Type Err,TypeError:expected bytes,not str

时间:2014-04-20 21:44:46

标签: 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

1 个答案:

答案 0 :(得分:6)

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

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