startswith TypeError in function

Solution 1:

It's because you're opening the file in bytes mode, and so you're calling bytes.startswith() and not str.startswith().

You need to do line.startswith(b'>'), which will make '>' a bytes literal.

Solution 2:

If remaining to open a file in binary, replacing 'STR' to bytes('STR'.encode('utf-8')) works for me.