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

TypeError a bytes-like object is required, not ‘str’ when writing to a file in Python 3 is a very common error in Python

The first time I came across this error I was so confused, the error usually happens when you I’ve very try to migrate to Python 3.5 from earlier versions of Python especially Python 2.

When It first happened The code was working fine in Python 2.7 and immediately after migrating to Python 3.5, I got the following error:

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

The code I was running is the following:

#
with open(fname, 'rb') as f:
    lines = [x.strip() for x in f.readlines()]

for line in lines:
    tmp = line.strip().lower()
    if 'some-pattern' in tmp: continue
    # ... code
#

When I first tried troubleshooting the problem, I tried a function called .decode(), I tried it on both sides of the statement above, but It did not work or solve the problem.

I also tried using find, but It did not work either:

#
if tmp.find('some-pattern') != -1: continue
#

I did not only encounter this error when moving from Python 2 to Python 3, I encountered a lot of other issues but I managed to solve all of them, this one was the only problem I could not solve.

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

After a lot of trial and error, I managed to solve the issue, bellow are possible solutions to your issue, in my case the first solution worked but I can not guarantee success for you, that is exactly why I added other possible solutions to the python 3 error.

Solution 1 : Open the file as a text file

After a while I realised that I opened the file in binary mode when I used ” open(fname, ‘rb’) as f “, which means that the data in my file will be returned as “bytes” instead of “str” objects. As a result you can only use bytes objects to test against tmp, so you need to replace the first line of code with the second line of code:

#
if b'some-pattern' in tmp: continue
#

If you do not want to do that you can instead replace 'rb' mode with 'r', this will allow you to open the file as a text file instead of a binary file.

That’s it guys this solution worked for me and will work for most people, if this does not work for you, just continue reading, I prepared other possible solutions for you.

Solution 2 : Open the file as a text file

It is worth to note that the bytes type was introduced to Python in the third version, the second version aka Python 2 did not have it, that is exactly why the code worked for me in Python 2 and in Python 3 it gave me the error above.

So where does our problem come from ?

Notice that we are comparing strings to bytes in the loop statement and that is our code fails and gives us the error.

The solution here is to decode the bytes when adding to our list, this is how our code should look like.

#
with open(fname, 'rb') as f:
    lines = [x.decode('utf8').strip() for x in f.readlines()]
#

Solution 3 : Encode the String Properly

As the error describes, in order to write a string to a file you need to encode it to a byte-like object first, and encode() is encoding it to a byte-string.

If you want to write the string to a file, you need to encode the string to a bytes object, you should do that first by using the encode()function, this is how you can use it :

#
'string text'.encode()
#

string text should be replaced with your own string

Solution 4 : Another way to use the Encode function

let us take this code, this code will throw the same error we have seen before.

#
for line in lines:
    print(type(line))# <class 'bytes'>
    if 'substring' in line:
       print('success')
#

the decode() function will solve the problem when used the right way, it solves the problem in this case:

                                                                       #
for line in lines:
    line = line.decode()
    print(type(line))# <class 'str'>
    if 'substring' in line:
       print('success')
                                                                       #

Summing-up

the typeerror a bytes like object is required not str when writing to a file in Python 3 ” is easy to solve for most people, the first solution works for most developers.

The problem is easy to identify unless if you have another issues that are hard to spot, I hope the solutions that I have provided worked for you, usually one of those solutions will do, thank you for reading.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/