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

25,181

TypeError implies that there is a mismatch in the datatype required vs given data's type. The function requires input of type 'bytes' while the code inputs data of type 'str'.

To convert the input string into byte-like object use str.encode function.

>>> string = "abcdef"
>>> type(string)
<class 'str'>
>>> string = string.encode('ascii')
>>> type(string)
<class 'bytes'>
Share:
25,181

Related videos on Youtube

KayaP
Author by

KayaP

I just love coding and i like to create fun apps :D

Updated on November 22, 2020

Comments

  • KayaP
    KayaP over 3 years

    i'm working to a personal project and i'm confronting a error: TypeError: a bytes-like object is required, not 'str'

    Here is my code: CLICK TO SEE THE CODE

    I want to make this script that is trying to find into the file a input text. Thanks!

    • Martijn Pieters
      Martijn Pieters over 4 years
      Sorry, we can't accept images of code, data or errors. Post those as text, so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers.