base64 Type Err, TypeError: expected bytes, not str

16,153

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

d64i = base64.b64encode(bytes(tmp, 'utf-8'))
Share:
16,153
saudi_Dev
Author by

saudi_Dev

Updated on June 06, 2022

Comments

  • saudi_Dev
    saudi_Dev almost 2 years

    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