nawawa001 发表于 2017-4-13 09:09:45

python php 只读文件变化的部分,特定的行开始读

应用场景:
一个文件在不停的从末尾写入,例如日志文件,但我们每次读的时候只需要读新增加的内容:
   思路:根据上次读的时候,记录下文件的位置,下次直接跳到这个位置,接着读,
用到的方法:seek()


#encoding=utf-8
'''
@authon:cooler
date:2012-04-28
'''
file = file("test.txt","r")
file.seek(9)
line=file.readline().strip()
while( line!=""):
print line + "|"+str(file.tell())
line=file.readline().strip()




$file = fopen("test.txt","r");
fseek($file,6);
while(!feof($file))
{
echo fgets($file)."| " .ftell($file) . " <br />";
}
fclose($file);
页: [1]
查看完整版本: python php 只读文件变化的部分,特定的行开始读