Python TypeError: A Bytes-Like Object Is Required, Not 'Str'

Python TypeError: A Bytes-Like Object Is Required, Not 'Str'

  1. the TypeError: a bytes-like object is required, not 'str' in Python
  2. Fix the TypeError: a bytes-like object is required, not 'str' in Python

In Python, the TypeError: a bytes-like object is required, not 'str' is raised when you are treating a bytes-like object as a string.

You cannot perform any actions on two different data types in a single statement, neither any arithmetic operations nor any logical operations. Otherwise, you will encounter the TypeError just like in this case we are facing the a bytes-like object is required, not 'str' error.

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

First, let us understand the error itself. It says that a bytes-like object is required, but we treat it as a string instead.

For further clarifications, let us understand the nature of the error through a practical example.

# open the file 'names.txt' in read-binary (rb) format
f = open("names.txt","rb")

# read lines
read_lines = [x.strip() for x in f.readlines()]

# loop through the lines
for line in read_lines:

    # print the type of line
    print(type(line))

# check some condition
    if "Zeeshan" in line:   # Error
        print(line)

# close the file
f.close()

Output:

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

In the above program, we read data from the file called names.txt in the rb (read-binary) format. Every piece of data in the next line of code is stored in the variable named read_lines.

Now, we are looping through each line from the read_lines and checking the condition if "Zeeshan" in line. This throws the TypeError: a bytes-like object is required, not 'str' because the text Zeeshan is a string, whereas the data type of the line is bytes which is against the Python syntax.

Let us understand it through another simple example. We will try to replace a string with a byte.

welcome_msg = b"Welcome to DelfStack"
print(welcome_msg)
print("The type of welcome msg is:\t",type(welcome_msg))

replace_msg = "Delfstack.com"
print("The type of msg is:\t\t",type(replace_msg))

# replace DelfStact with Delfstack.com
replace_welcome_msg = welcome_msg.replace("DelfStack",replace_msg)
print(replace_welcome_msg)

Output:

b'Welcome to DelfStack'
The type of welcome msg is:	 <class 'bytes'>
The type of msg is:			 <class 'str'>

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

In this example, you can see the values have different data types. One is bytes, and the other is str.

You cannot replace them with each other, and if you do so, you will get the TypeError: a bytes-like object is required, not 'str', as demonstrated in the above example.

Fix the TypeError: a bytes-like object is required, not 'str' in Python

There are multiple solutions to avoid the TypeError. You can typecast the data or change the file opening format to r or any other format that returns the data as a string so you can use it accordingly.

Let’s say you are reading data from a file and encounter the TypeError: a bytes-like object is required, not 'str'. To fix this error, you have either the file format or type cast the string into bytes and compare it with the binary format.

Let’s see an example and fix this TypeError.

# open the file 'names.txt' in read-binary (r) format
f = open("names.txt","r")

# read lines
read_lines = [x.strip() for x in f.readlines()]

# loop through the lines
for line in read_lines:

# check some condition
    if "Zeeshan" in line:
        print(line)

# close the file
f.close()

Output:

Zeeshan

The TypeError is fixed, and the program is smoothly executed because we compared a string Zeeshan with another string from the file and the string Zeeshan is in the file names.txt.

Similarly, you can replace a byte with another byte, as shown in the example below.

msg = b"Hi Zeeshan"
replace_msg =  msg.replace(b"Zeeshan",b"Zeeshan Afridi")
print(replace_msg)

print("Data type of msg:\t\t",type(msg))
print("Data type of replace_msg:\t",type(replace_msg))

Output:

b'Hi Zeeshan Afridi'
Data type of msg:            <class 'bytes'>
Data type of replace_msg:    <class 'bytes'>

Since the data type of both msg and replace_msg is bytes, so we can replace them with each other.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10