I\'m trying to read a binary file and am getting confusing results.
f = open(\'foo.dat\',\'r\')
data = f.read()
print len(data), f.tell()
It's probably on Windows and you have a Ctrl-Z (the CP/M end-of-file marker, which was inherited by Windows via MS-DOS). Do this:
f = open('foo.dat','rb') # NOTE b for binary
data = f.read()
print len(data), f.tell()
print repr(data[60:70])
and show us the output. Ctrl-Z is '\x1a'
aka chr(26)
.
data
is a str
object, thus len(data)
tells you about the length of this string, not the file length in bytes.