我正在尝试逐个读取文件:
def buf_read2(filename, buffer_size):
with open(filename, 'rb') as f:
buffer = f.read(buffer_size)
print buffer # and do other stuff with it
这不会读取文件的最后一块。例如,如果一个文件是 129 字节,而我设置buffer_size
为 128,则不会读取最后一个字节。
这种老派的方法虽然有效:
def buf_read1(filename, buffer_size):
f = open(filename, 'rb')
while True:
buffer = f.read(buffer_size)
if not buffer:
break
print buffer # and do other stuff with it
f.close()
我究竟做错了什么?