TypeError: a bytes-like object is required, not str error
TypeError: a bytes-like object is required, not str error occurs in Python when we try to use string where a byte object is required. This article will walk you through the causes of this TypeError error and also we will explore different ways to fix it.

Causes and Solutions of TypeError: a bytes-like object is required, not str error
There are various reasons behind this error, let’s see a few of them with their solutions one by one.
Reason 1: Encoding issues
Usually, this error occurs when we try to execute the same code on different versions of Python. For example, if we run this code on Python 2 it will run without any error but if we execute it on Python 3 it may throw the TypeError: a bytes-like object is required, not str error. Because these two version handle strings differently.
Note: The bytes objects represent binary data, whereas strings represent text data.
Like in Python 2 strings are treated as bytes, that’s why it can be used interchangeably with byte objects. It allows the conversion between the bytes and Unicode objects automatically. Whereas, in Python 3 strings are treated as Unicode objects and it does not allow automatic conversion between bytes and Unicode objects. So, if a string object is given to a function where a byte object was expected a type error can occur.
import zlib my_string = "hello, world!" compressed = zlib.compress(my_string) print("The compressed data is:", compressed)
Output
TypeError: a bytes-like object is required, not 'str'
In this example, the zlib module is used in Python to compress a string object my_string containing the text “hello, world!”.
The my_string object is passed as an input argument to the zlib.compress() function, which expects a byte object as input. Since we haven’t explicitly converted my_string to a byte object, Python throws the TypeError: a bytes-like object is required, not str error.
Solution: Encode the string to a bytes object
To fix this issue, use the encode() method on the string object with the appropriate encoding scheme to convert the string into a byte object explicitly before passing it to the function as shown in the below example:
import zlib
my_string = "hello, world!"
compressed = zlib.compress(my_string.encode())
print("The compressed data is:", compressed)
Output
The compressed data is: b'x\x9c\xcbH\xcd\xc9\xc9\xd7Q(\xcf/\xcaIQ\x04\x00!\xfe\x04\xaa'
We can observe now, the code runs without raising the TypeError error. Here, the encode() method is used to convert the string object my_string to a byte object before passing it to the zlib.compress.
Reason 2: Binary Data Handling:
This error can also occur while working with binary data such as reading or writing files. One of the common scenarios is when we treat a text file as a binary file as shown in the below example:
file_path = 'D:\example.txt'
with open(file_path, 'wb') as f:
f.write("hi")
Output
TypeError: a bytes-like object is required, not 'str'
In this example, a file example.txt located at ‘D:\example.txt’ is opened using an open() function in write binary mode. Here, the wb represent the binary writing mode. Then, the with statement is used to ensure that the file will close automatically the code block executes successfully.
Next, the write method expects a binary object because the file is opened in binary mode but we passed a string object hi instead which raised TypeError: a bytes-like object is required, not ‘str’.
Solution: Use a Bytes Object Instead of a String
To fix this issue, use byte object instead of a string when working with binary data like reading or writing file
file_path = 'D:\example.txt'
with open(file_path, 'wb') as f:
f.write(b'hi')
In the above code, in the write function b prefix is used to convert string to byte object which can then be written to the file in binary mode (‘wb’). Now, hi is successfully written in the example.txt file in the binary format.
Another possible solution to fix this error is open the file in write mode w instead of binary write wb mode.
file_path = 'D:\example.txt'
with open(file_path, 'w') as f:
f.write('hi')
We can see, now our code executes successfully without any error. Here, the write mode w is used to write a string object to a file.
Conclusion:
In Python, the TypeError: a bytes-like object is required, not str error occurs when we try to use a string object where a byte object is expected. This can happen when working with files, compressed data, or other binary data formats. To fix this error, we need to make sure we pass a byte object to the function or method that expects it. This can be done by explicitly converting a string object to a byte object using the b prefix or the encode()method.
It is also important to keep in mind that Python 3 treats strings as Unicode by default, whereas Python 2 treats strings as bytes by default. Therefore, code that works in Python 2 may not work in Python 3, and vice versa. This can also lead to the TypeError error if we’re not careful with our string and byte object handling.
Good Luck with your Learning !!
Related Topics:
Fix – TypeError: tuple indices must be integers or slices not str
TypeError: list indices must be integers or slices not tuple
FIX – TypeError: list indices must be integers or slices not float