我想打开一个文件并使用读取每一行f.seek()
and f.tell()
:
测试.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
它读取整个文件。
好的,你可以使用这个:
f = open( ... )
f.seek(last_pos)
line = f.readline() # no 's' at the end of `readline()`
last_pos = f.tell()
f.close()
只要记住,last_pos
不是文件中的行号,它是距文件开头的字节偏移量——增加/减少它是没有意义的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)