|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# 51CTOBG: http://xmdevops.blog.51cto.com/
# Purpose:
#
"""
from __future__ import absolute_import
# 说明: 导入公共模块
import os
import sys
import time
import chardet
# 说明: 导入其它模块
#
if __name__ == '__main__':
seekps = 0;
target = 'data.txt';
try:
# 指针跳到最后作为起读位置
with open(target, 'r+b') as f:
f.seek(0, os.SEEK_END)
seekps = f.tell()
print 'notice: file length is', seekps
rest_lines = []
# 默认读取15行
line_reads = 15
# 假设默认每行1000字节
line_bytes = 1000
'''
1. 当f_length < line_bytes,就seek到0开始读取,读完break
2. 当f_length > line_bytes,换行符数n表示n+1行
=> 当n+1 >= line_reads,取出其中前line_reads个元素
=> 当n+1 < line_reads,继续向前seek到count*line_reads位置读取
'''
count = 1
while True:
if seekps <= line_bytes*count:
f.seek(0)
rest_lines = f.read().split(os.linesep)[-line_reads:]
break
f.seek(-1*line_bytes*count, 2)
rest_lines = f.read().split(os.linesep)
if len(rest_lines)>=line_reads:
rest_lines = rest_lines[-line_reads:]
break
else:
count += 1
for line in rest_lines:
code = chardet.detect(line).get('encoding')
line = line.decode(code).encode(sys.stdout.encoding)
sys.stdout.write(''.join([line.strip(), os.linesep]))
sys.stdout.flush()
except Exception, e:
print 'notice: open file with error({0})'.format(e)
exit()
print 'notice: start position is', seekps
while True:
try:
with open(target, 'r+b') as f:
f.seek(0, os.SEEK_END)
# 防止数据被意外截断
if f.tell()<seekps:
f.seek(f.tell())
else:
f.seek(seekps)
while True:
line = f.readline()
if not line.strip():
break
code = chardet.detect(line).get('encoding')
line = line.decode(code).encode(sys.stdout.encoding)
sys.stdout.write(''.join([line.strip(), os.linesep]))
sys.stdout.flush()
seekps = f.tell()
except Exception, e:
print 'notice: open file with error({0})'.format(e)
break
time.sleep(0.1) |
|
|