Python3 throws the TypeError: a bytes like object is required, not ‘str’, when we try to do string operations on binary values. It generally appears when you open a file in binary mode. Consider this code –
with open("superhero.txt", 'rb') as myFile:
lines = [x.strip() for x in myFile.readlines()]
PythonCopy
In this code, we are opening superhero.txt
file in 'rb'
mode, which means that it is read only and binary. Now suppose we do operations like string find, then that will fail because we have binary data and not string data.
#python #python error #python string #python-short