What's The Type Of Read() Function's Return Value?
Solution 1:
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.
Solution 2:
- What's the type of a return value in read() function?
You mean the method read
of type file
. The command help(file.read)
gives:
read([size])
-> read at most size bytes, returned as a string.If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given.
- How can I get the first character in a string or array?
Just like you did, [0]
.
Solution 3:
A suggestion when using sequence types: instead of using the exact index, it could be more useful using a slice. A slice always exists, even if the object is empty.
buf[0:1] returns the same of buf[0] when non empty, and it returns a empty string '' if buff is an empty string, while buf[0] returns an error (since it is empty, the first character doesn't exist)
And a string is False only when empty
Post a Comment for "What's The Type Of Read() Function's Return Value?"