有人知道为什么当你以这种方式迭代文件时:
输入:
f = open('test.txt', 'r')
for line in f:
print "f.tell(): ",f.tell()
输出:
f.tell(): 8192
f.tell(): 8192
f.tell(): 8192
f.tell(): 8192
我不断地从tell()获取错误的文件索引,但如果我使用readline,我会得到适当的tell()索引:
输入:
f = open('test.txt', 'r')
while True:
line = f.readline()
if (line == ''):
break
print "f.tell(): ",f.tell()
输出:
f.tell(): 103
f.tell(): 107
f.tell(): 115
f.tell(): 124
顺便说一下,我正在运行 Python 2.7.1。