TypeError: 'str' does not support the buffer interface
You're using Python 3, where there is a strict division between text (str
) and data (bytes
). Text can't be written to a file if you don't explicitly encode it first.
There are two ways to do this:
1) Open the file in text mode (possibly with an encoding specified) so that strings are automatically encoded for you:
with open("lt.txt", 'at', encoding='utf8') as outfile:
outfile.write(hash_digest + '\n') # or print(hash_digest, file=outfile)
If you don't specify the encoding yourself when opening the file in text mode, the default encoding of your system locale would be used.
2) Encode the strings manually like you tried. But don't try to mix str
with bytes
like you did, either use a byte literal:
hash_digest = hash_digest.encode('utf-8')
with open("lt.txt", 'ab') as outfile:
outfile.write(hash_digest + b'\n') # note the b for bytes
or encode after adding the newline:
outfile.write((hash_digest + '\n').encode('utf-8'))