我在文件中使用seek - 文件中有一堆文件名和文件上的一些进程日志 - 其中一些日志有错误。我一行一行,如果我得到一个错误,我想记录两个文件名之间的所有内容。seek()在文件处理中的问题
当我使用seek时,我认为不是将它移动到我想要的行,而是将它移动到字符#。例如
f=open("fileblah",'r')
while f:
line=f.readline()
counter=counter+1
f.seek(tail_position) # i want the next loop to start from after the error happened.
if line.startswith("D:")
header_position=counter
error_flag=0 #unset error flag
if line.startswith("error")
error_flag=1 #set error_flag
while(not(line.startswith("D:"): #go until next file beginning
line=f.readline()
counter=counter+1
tail_position=counter #have come to the next filename
我可以看到这是非常低效的,但它并没有在所有的工作,因为f.seek(tail_position)移动文件指针到字符#,而不是线#