当你使用f.next()迭代文件时,Python的f.tell不能像我预期的那样工作:
>>> f=open(".bash_profile", "r")
>>> f.tell()
0
>>> f.next()
"alias rm='rm -i'\n"
>>> f.tell()
397
>>> f.next()
"alias cp='cp -i'\n"
>>> f.tell()
397
>>> f.next()
"alias mv='mv -i'\n"
>>> f.tell()
397
Run Code Online (Sandbox Code Playgroud)
看起来它给你缓冲区的位置,而不是你刚接下来的位置().
我以前使用seek/tell 技巧在使用readline()迭代文件时回绕一行.使用next()时有没有办法倒回一行?
请解释以下内容:
def feed(data):
import os
print "DATA LEN: %s" % len(data)
f = open("copy", "w")
f.write(data)
f.close()
print "FILE LEN: %s" % os.stat("copy").st_size
t = tempfile.NamedTemporaryFile()
t.write(data)
print "TEMP LEN: %s" % os.stat(t.name).st_size
t.close()
feed(x)
DATA LEN: 11004
FILE LEN: 11004
TEMP LEN: 8192
Run Code Online (Sandbox Code Playgroud)
为什么会有所不同,我可以解决温度问题吗?结局似乎被砍了。
在2.6、2.7上测试