python - Attempting to Base64 encode this variable -
i'm trying encode 'username' variable base64, write text file, , decode base64, , read/print it.
while true: username = input("what username?: ") file = open("newfile.txt", "w") file.write(base64.b64encode(username)) file.close file = open("newfile.txt", "r") file.read(base64.b64decode(username)) break
-typeerror- 'str' not support buffer interface
what did here seemed logical out of i've seen.
i new python, , have tried method's i've seen online base64 encode variable, , none have worked.
base64 expects , returns bytes (in python3); strings must written files. here example explicit writing , more compact reading:
import base64 while true: username_str = input("what username?: ") open("newfile.txt", "w") file_handler: username_bytes = bytes(username_str, 'utf-8') b64_bytes = base64.b64encode(username_bytes) b64_str = b64_bytes.decode('utf-8') file_handler.write(b64_str) # file_handler.close() # close not needed inside context handler open("newfile.txt", "r") file_handler: print(base64.b64decode(bytes(file_handler.read(), 'utf-8')).decode('utf-8')) break
btw: file reserved keyword , should not used variable.
Comments
Post a Comment