3

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 here? Only the first 61 bytes are read, but the file object is telling me that I'm at the end of the file (the file is 600 bytes long). What happened to the rest of the file?

I just tried reading it in Matlab and it read it in fine so I'm pretty sure the data file is ok.

The documentation mentions something about blocking: "Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given." Am I in non-blocking mode? Seems like that shouldn't matter for a file. How do I switch to blocking mode?

UPDATE @John Machin - Yup! Thank you! Looks like that is indeed what was going on. Here's the output:

600, 600
'm\x1aN\x16\x8d\x1e\x96\x10h\x1a'

The '\x1a' is definitely in there.

1
  • 2
    'r' implies opening a file in text mode, when some things, like line endings, are inteerpreted. Try 'rb' just to make sure. Commented Dec 9, 2011 at 3:56

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

@EpicAdv: The way it works here is to edit your question to add your output instead of editing an answer here, as your output is related to the question itself. (I see T.Rob has just done this for you, but anyway, this is how you do it in future.)
Thanks! I'm getting used to the format and am very appreciative of the helpful community.
I had the same problem open('foo','r') just doesn't work the same in Windows.
-1

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

1 Comment

This is true but does not answer the question. f.read() should read the entire contents of the file, so one would expect the two values to be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.