Typeerror: 'Str' Does Not Support the Buffer Interface

TypeError: 'str' does not support the buffer interface

If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it).

plaintext = input("Please enter the text you want to compress")
filename = input("Please enter the desired filename")
with gzip.open(filename + ".gz", "wb") as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))

Also do not use variable names like string or file while those are names of module or function.

EDIT @Tom

Yes, non-ASCII text is also compressed/decompressed. I use Polish letters with UTF-8 encoding:

plaintext = 'Polish text: ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
filename = 'foo.gz'
with gzip.open(filename, 'wb') as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))
with gzip.open(filename, 'r') as infile:
outfile_content = infile.read().decode('UTF-8')
print(outfile_content)

TypeError: 'str' does not support the buffer interface in python

You're running a python 2 script with python 3. Python 3 now returns bytes no longer str when reading from a binary stream.

3 choices:

  1. run it with python 2. That if you don't have the rights/time to adapt the script, not recommended as python 3 is becoming more and more the norm.

  2. change your code to insert a decode function (it will continue to work in python 2):

username = cred_file.readlines()[0].decode().split(';')[0]

If file is opened in read/binary mode, readlines returns a list of bytes not str. You have do decode the bytes into a str to apply str methods.


  1. open the file in "r" instead of "rb". readlines then returns a list of str and your code will work. Sometimes it can be problematic on windows because of need to preserve the carriage return (\r) chars, so look out for side effects in your code.

Note: cred_file.readlines()[0] is a questionable construction: you're reading the whole file lines, and drop all the lines but the first. Not very efficient I/O and CPU wise.
Prefer that: cred_file.readline() which is equivalent to read the first line.
If you need to read all the lines for further processing, then store the result of readlines in a list.

TypeError : 'str' does not support the buffer interface

Just guessing since you didn't show the stack trace, or even the line that generates the error. But I think it can be fixed:

s.write((l + '\n').encode("utf8"))

TypeError: 'str' does not support the buffer interface using Python3.x

This happens because bytes in python 3 are not same as str. What the request.recv gives you is a bytes of data. You'd need to first convert it to str and then use it to split.

You can do an utf-8 decode. So something like -

self.data.decode('utf-8').split(',') should work. 

How to convert between bytes and strings in Python 3? has a more detailed explanation.

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'))

Python: 'str' does not support the buffer interface on subprocess.communicate (migrating to 3.x)

In python 3, subprocess streams are binary.

To write a string, just encode a binary, in your case ascii codec is OK:

fdisk_cmd.communicate(cmd_a.encode("ascii"))

Any ideas how to fix TypeError: 'str' does not support the buffer interface?

The error means that you are trying to pass str object (Unicode text) instead of binary data (byte sequence):

>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface

Python 3.5 improves the error message here:

>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

To save text as binary data, you could encode it using a character encoding. To compress the data, you could use gzip module:

import gzip
import io

with io.TextIOWrapper(gzip.open('sentence.txt.gz', 'wb'),
encoding='utf-8') as file:
print(sentence, file=file)


Related Topics



Leave a reply



Submit