what's the type of read() function's return value in python?
Question: I write a program in python here. I wanna read a binary file, read first 188
Bytes, and check whether the first character is 0x47. Code below:

    
    
    import os
    fp=open("try.ts","rb")
    for i in range(100):
      buf=fp.read(188)
    if buf[0]=="\x47"
      print "OK"
    fp.close()
    

But it goes wrong as follows:

    
    
       if buf[0]=="\x47"
                    ^
    SyntaxError: invalid syntax
    

What I wanna ask are:

  1. What's the type of a return value in read() function?
  2. How can I get the first character in a string or array? 

* * *

Thanks, I miss the colon. But when I add it,it shows that:

    
    
        if buf[0]=="\x47":
    IndexError: string index out of range
    

Still wrong ?


Answer:   1. `str`
  2. You forgot the colon: 

`if buf[0] == "\x47":`

* * *
    
    
        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.

