TypeError: a bytes-like object is required, not 'str'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: a bytes-like object is required, not 'str' #

To solve the Python "TypeError: a bytes-like object is required, not 'str'", encode the str to bytes, e.g. my_str.encode('utf-8'). The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

typeerror typeerror bytes like object is required not str

If you got the error when using the socket module, scroll down to the next section.

Here is an example of how the error occurs.

main.py
# 👇️ open file with open('example.txt', 'rb') as f: result = f.readlines() # ⛔️ TypeError: a bytes-like object is required, not 'str' if 'first line' in result[0]: print('success')

We opened the file in binary mode (using the rb mode), so the result list contains bytes objects.

The error is caused because we can't check if a string is contained in a bytes object as the two values are of different types.

To solve the error, use the encode() method to encode the str into bytes.

main.py
with open('example.txt', 'rb') as f: result = f.readlines() # 👇️ call encode() on the string if 'first line'.encode('utf-8') in result[0]: # ✅ this runs print('success')

Now the values on both sides of the in operator are bytes objects, so the error is resolved.

Note that we could have also decoded the bytes object into a string.

main.py
with open('example.txt', 'rb') as f: result = f.readlines() # ✅ decode bytes into str if 'first line' in result[0].decode('utf-8'): print('success')

The values on the left and right-hand side of the in operator are of the same type (str).

The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8.

You can also use a list comprehension to perform the operation on each element in a list.

main.py
with open('example.txt', 'rb') as f: lines = [l.decode('utf-8') for l in f.readlines()] if 'first line' in lines[0]: print('success')

The example shows how to decode each bytes object in the list to a string.

The error is also caused when sending data to a socket with the socket module.
main.py
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('example.com', 80)) # ⛔️ TypeError: a bytes-like object is required, not 'str' s.send('GET https://example.com/example.txt HTTP/1.0\n\n') while True: data = s.recv(1024) if not data: break print(data) s.close()

One way to solve the error is to encode the str to a bytes object by using the encode() method.

You should also make sure to use the sendall() method instead of send(), because sendall() continues to send data from bytes until either all data has been sent or an error occurs.

main.py
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('example.com', 80)) # ✅ call .encode('utf-8') on the string 👇️ s.sendall( 'GET https://example.com/example.txt HTTP/1.0\n\n'.encode('utf-8') ) while True: data = s.recv(1024) if not data: break print(data) s.close()
We used the encode method to encode the string into bytes, which solves the error.

If you have a string literal, you can simply prefix it with b which achieves the same result.

main.py
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('example.com', 80)) # ✅ prefix string literal with b'' s.sendall(b'GET https://example.com/example.txt HTTP/1.0\n\n') while True: data = s.recv(1024) if not data: break print(data) s.close()
Note that this approach can only be used with string literals. If the string is stored in a variable, use the .encode('utf-8') method instead.

The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

Conclusion #

To solve the Python "TypeError: a bytes-like object is required, not 'str'", encode the str to bytes, e.g. my_str.encode('utf-8'). The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.