Solving Error in subprocess “TypeError: a bytes-like object is required, not ‘str'”

subprocess “TypeError: a bytes-like object is required, not ‘str'” is an error which occurs in Python to anyone who confuses object types and does not encode them properly.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Exploring the Error : subprocess “TypeError: a bytes-like object is required, not ‘str'”

The error can happen to anyone using python who confuses object types and does not encode them properly.

This is the most important line of the error minus the Traceback.

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

Bellow you can find the solution which has worked for me, plus other popular possible solutions among developers who already faced this error.

Solution : encode into a byte array object

The solution is simple, you should encode into a byte array object.

Before you do that, please make sure to see if your error message is similar to the error message bellow.

                                                                       #
Traceback (most recent call last):
  File "B:\Program Files\ffmpeg\bin\isis.py", line 7, in <module>
...
...
  File "B:\Program Files\ffmpeg\bin\isis.py", line 6, in <listcomp>
    return [T for T in result.stdout.readlines() if "Timing" in T]
TypeError: a bytes-like object is required, not 'str'
                                                                       #

We get the error after running the code bellow.

                                                                       #
import subprocess
def getLength(filename):
  result = subprocess.Popen(["ffprobe", filename],
    stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
  return [T for T in result.stdout.readlines() if "Timing" in T]
....
                                                                       #

As you can see above, T and “Timing” are not of the same type. T is a byte like object and “Timing” is a string. While results.stdout.readlines() works with bytecode and not strings.

You need to store the string in our case “Timing” in a variable. And encode the variable using .encode('utf-8'). Exactly like in the example bellow.

                                                                       #
stringvariable = "Timing"
stringvariable.encode('utf-8')
                                                                       #

The solution should be enough to solve the issue.

If you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

Thi is the end of our article, a fresh install of pyaudio if done the proper way is able to solve this issue for most developers including me, I hope you found our article and website useful, never give up, keep creating and keep coding.

Errors are normal in our field, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/