开发者

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

开发者 https://www.devze.com 2023-01-24 03:13 出处:网络
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.

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

精彩评论

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