Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 470
KayaP Avatar asked Feb 07 '26 23:02

KayaP


1 Answers

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'>
like image 55
kmmanoj Avatar answered Feb 09 '26 13:02

kmmanoj