I understand that there are similar questions, but I do not understand them so here goes: I have bytes (encoded from a string) in a string form (read from a file). When I try to decode the bytes, I get an error saying that it is a string, not bytes. I understand it is in the wrong form, but do I not have the correct information? How can I convert bytes in a string back to bytes? I will also note I know this is not a secure password method and will not be used as one. (I am using python 3)
I've done some research into how I can fix this, but I am very new and either did not understand it or could not apply it. I though this would work but it does not:
password=bytes(password, 'cp037')
Oh well. Here is a short version of the code I have:
#writing to the file
password="example"
password=password.encode('cp037')
password=str(password)
f=open("passwordFile.txt", "w+")
f.write(password)
f.close
#reading from the file
f=open("passwordFile.txt","r")
password=f.read()
#this is where I need to turn password back into bytes
password=password.decode('cp037')
print(password)
I expected to get example as output, but have a error: AttributeError: 'str' object has no attribute 'decode'
ywhen you try to decode itpassword=str(password)? That doesn't decode the string; it just gives you a string representation of the bytes.cp037as the encoding, and write the original password as-is, letting the file handle do the encoding for you.password = 'example'; with open("passwordFile.txt", "w+", encoding='cp037') as f: f.write(password).