开发者

Python read() seems to return less data than it reads

Can anyone tell me why the length of data is much less than the position of the end of the file? I would have expected these to be equal.

>>> target = open('target.jpg')
>>> print target.tell()
0
>>&g开发者_StackOverflow社区t; data = target.read()
>>> print target.tell()
40962
>>> print len(data)
52


Open the file in binary mode:

target = open('target.jpg','rb')

I would not trust tell() on a file not opened as binary.


Later: actually, on reviewing the comments, I should have said I would not trust a read on a binary file opened as text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜