【发布时间】:2013-03-24 03:22:46
【问题描述】:
我想打开一个文件并使用f.seek() 和f.tell() 读取每一行:
test.txt:
abc
def
ghi
jkl
我的代码是:
f = open('test.txt', 'r')
last_pos = f.tell() # get to know the current position in the file
last_pos = last_pos + 1
f.seek(last_pos) # to change the current position in a file
text= f.readlines(last_pos)
print text
它读取整个文件。
【问题讨论】:
-
是的,
readlines就是这样做的。你的问题到底是什么? -
我需要逐行读取,将last_pos保存在某处,关闭文件,去打开文件,寻找last_pos,读取行,更新last_pos,关闭文件...
-
@John,如果您在子进程之间传递数据,请查看 StringIO 等。或者考虑使用数据库,例如MySQL