I wanna read first 188 bytes from a binary file, and check whether the first character is 0x47
. Code below:
import os
fp=open(\"try.ts\",\"rb\")
for
if buf[0]=="\x47":
IndexError: string index out of range
That means your buf
is empty. You overwrote it 100 times in your loop. The file probably doesn't have 18800 bytes in it. At the end of a file read
just returns an empty string. Did you mean to put your if
inside the for
? If so, indent it accordingly.