You cannot access a bytes-like object like a string, for example, if you try to replace characters or perform a character-based search on a bytearray. If you perform an operation for a string on a bytes-like object, you will raise the error: TypeError: a bytes-like object is required, not ‘str’.

This tutorial will go through the error in detail and an example scenario to learn how to solve it.


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

What is a TypeError?

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type. The particular error message tells us we are treating a value like a string rather than like a bytes-like object. Byte-like objects are distinct from strings, and you cannot manipulate them like a string.

What is a Bytes-like Object?

Any object that stores a sequence of bytes qualifies as a bytes-like object. These objects include bytes, bytearray, array.array. Str is a Python text type. The characters are not in a specific encoding, so you cannot directly use them as raw binary data, and you have to encode them first. To create byte objects, we can use the bytes() function. Let’s look at an example of converting a string to a bytes object. We pass the string as the first argument, and the second is the encoding we want to use.

my_string = "researchdatapod.com"

print("The given string is: ", my_string)

bytes_obj = bytes(my_string, "UTF-8")

print("The bytes object is: ", bytes_obj)

print("The size of the bytes: ", len(bytes_obj)
The given string is:  researchdatapod.com

The bytes object is:  b'researchdatapod.com'

The size of the bytes:  19

Example

If you try to open a file as a binary file instead of a text file, you can encounter this error. Let’s look at an example where we try to read a text file storing the names of pizzas and print the pizzas that contain chicken in the name to the console.

First, let’s create our text file called pizzas.txt:

margherita
pepperoni
four cheeses
ham and pineapple
chicken and sweetcorn
meat feast
chicken fajita

Once we have the pizzas.txt, we can make a program that reads the file and prints the names with chicken in the name to the console:

with open("pizzas.txt", "rb") as f:

    pizzas = f.readlines()

for pizza in pizzas:

    if "chicken" in pizza:

        print(pizza)

The above code opens up the file pizzas.txt and reads its contents to a variable called pizzas. The pizzas variable stores an iterable object consisting of each line in the pizzas.txt file. Next, we can use a for loop to iterate over each pizza in the list and check if each line contains “chicken”. If the line has the word “chicken”, the program prints the line to the console; otherwise, nothing happens. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      3 
      4 for pizza in pizzas:
      5     if "chicken" in pizza:
      6         print(pizza)
      7 

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

We get this error because we a trying to access a bytes-like object as if it were a string. The source of the error is: with open(“pizzas.txt”, “rb”) as f:. The b in rb tells Python to open the pizzas.txt file as a binary. The Python interpreter treats binary files as a series of bytes, so if we check if “chicken” is in each line in the file, Python cannot check for a string in a bytes object, so the interpreter raises the error.

Solution #1: Read mode

We can solve this error by opening the file in read-mode instead of binary read mode. We do this by using “r” instead of “rb” in the with open… line. Let’s look at the revised code:

with open("pizzas.txt", "r") as f: 

    pizzas = f.readlines()

    for pizza in pizzas:

        if "chicken" in pizza:

            print(pizza)

Let’s run the code to see what happens:

chicken and sweetcorn

chicken fajita

The code runs successfully and prints the pizzas containing chicken in the name.

Solution #2: Decode

The decode() method is built-in to Python and converts an object from one encoding scheme to another. By default, the decode() method uses the “UTF-8” encoding scheme for conversion. Let’s look at the revised code with the decode() method:

with open("pizzas.txt", "rb") as f:

    pizzas = [x.decode() for x in f.readlines()]

    for pizza in pizzas:

        if "chicken" in pizza:

            print(pizza)

The above program opens the file as a binary and decodes every line from bytes to str. The program then iterates over each line and prints the lines that contain the word chicken. Let’s run the code to see what happens:

chicken and sweetcorn

chicken fajita

The code runs successfully and prints the pizzas containing chicken in the name.

Summary

Congratulations on reading to the end of this tutorial! The error “TypeError: a bytes-like object is required, not ‘str'” occurs when you try to call a string as if it were a function. A common source of the error is trying to read a text file as a binary. To solve this error, you can either open the file in read-mode or use decode() to convert a bytes-like object to str type.

For further reading about strings, bytes string, and encodings, go to the article: What is the ‘u’ Before a String in Python?

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!