Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 Type Err, TypeError: expected bytes, not str

when I try this code:

#!/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")

I got this is error:

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
like image 701
saudi_Dev Avatar asked Nov 24 '25 15:11

saudi_Dev


1 Answers

Well it is what it says. base64.b64encode() takes bytes as an argument, not a string.

d64i = base64.b64encode(bytes(tmp, 'utf-8'))
like image 91
Alexander Ejbekov Avatar answered Nov 27 '25 04:11

Alexander Ejbekov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!