Skip to content Skip to sidebar Skip to footer

Python Stops Reading File Using Read

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() The output is: 61, 600 What is going on her

Solution 1:

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() 
printlen(data), f.tell() 
printrepr(data[60:70])

and show us the output. Ctrl-Z is '\x1a' aka chr(26).

Solution 2:

data is a str object, thus len(data) tells you about the length of this string, not the file length in bytes.

Post a Comment for "Python Stops Reading File Using Read"