python实现tailf
# -*- coding:utf-8 -*-
'''
Created on 2016年10月28日 @author: zhangsongbin
''' import time
class file_read:
def __init__(self,logfilename):
self._logfilename=logfilename def file_readlines(self,line):
print line # def file_readline(self):
def file_readline(self):
f=open(self._logfilename,'r')
f.seek(0,2) #直接到文件结尾
while True:
offset=f.tell()#得到现在的偏移量
line=f.readline()#得到现在偏移量后面一行的内容,这里如果下一行是空的话,偏移量不会变大;如不是空,偏移量会变大。
if not line:
f.seek(offset)#设置新的偏移量
time.sleep(20)
else:
self.file_readlines(line) f.close() if __name__ == '__main__':
a=file_read('./trace_order.log')
a.file_readline()
一个简单的tailf程序,这个程序后续可以拿来做日志分析,目前还有个缺点就是,这个文件如果被删除了,即使新建一个一样的名字的问题,也得把程序再跑一边,后续会优化。
优化版本如下:
# -*- coding:utf-8 -*-
'''
Created on 2016年10月28日 @author: zhangsongbin
'''
import os.path
import time
class test(object):
pass
class file_read:
def __init__(self,logfilename):
self._logfilename=logfilename
def get_time(self,_logfilename):#得到ctime的方法,里面做了些判断
if os.path.exists(_logfilename):
return time.ctime(os.path.getctime(_logfilename))
else:
print str(self._logfilename)+'is not exist,after 1s system will check again'
time.sleep(1)
self.get_time(_logfilename)
def file_readlines(self,line):
print line def file_readline(self):
if os.path.exists(self._logfilename):#检测文件是否存在
f=open(self._logfilename,'r')
else:
print str(self._logfilename)+'is not exist,after 1s system will check again'
time.sleep(1)
self.file_readline()#重新调用自己这个方法,再次检测文件 f.seek(0,2) #到文件结尾
#before_ctime=time.ctime(os.path.getctime(self._logfilename)) #得到文件的目前的状态时间
before_ctime=self.get_time(self._logfilename)
while True:
offset=f.tell()#得到现在的偏移量
line=f.readline()#得到现在偏移量后面一行的内容,这里如果下一行是空的话,偏移量不会变大;如不是空,偏移量会变大。
if not line:
#after_ctime=time.ctime(os.path.getctime(self._logfilename)) #得到文件当line是空值的状态时间
after_ctime=self.get_time(self._logfilename)
after_offset=f.tell() #得到当line为空的offset
if offset == after_offset and before_ctime != after_ctime and os.path.exists(self._logfilename): #当文件状态时间不一致时,而offset又每次都一样就表示之前的log文件被删除了,我们要重新打开一次log文件
f.close()
f=open(self._logfilename,'r')
line=f.readline() #log的第一行
self.file_readlines(line) #打印第一行,然后跳过本次循环到while true
before_ctime=self.get_time(self._logfilename) #从新去得到before_ctime
continue
time.sleep(0.1)#这个sleep比较重要,如果log文件生成的速度很快,time值就小点,如果生成时间长就写大点。
else:
self.file_readlines(line)
#before_ctime=time.ctime(os.path.getctime(self._logfilename)) #当line不为空表示log正常在读出来了,重新获取一次log的状态时间
before_ctime=self.get_time(self._logfilename) f.close() if __name__ == '__main__':
a=file_read('./echo_log.log')
a.file_readline()
模拟日志生成的shell脚本:
#!/bin/sh
n=
while true;
do
echo $(date +%Y-%m-%d\;%H:%M:%S) >> echo_log.log
echo $(date +%Y-%m-%d\;%H:%M:%S)
let n=$n+
sleep 0.5
if (( $n == ));then
rm -rf echo_log.log
n=
fi
done
大家可以测试下,先开shell脚本生成日志,再用优化过后的Python程序读出日志。
python实现tailf的更多相关文章
- 利用pyinotify监控文件内容,像tailf命令但比它更强
Linux的tail/tailf命令使用了内核提供的inotify功能,下面的Python例子也使用inotify实现比tail/tailf更强的监控文件功能. watchfile.py #!/usr ...
- python基础—迭代器、生成器
python基础-迭代器.生成器 1 迭代器定义 迭代的意思是重复做一些事很多次,就像在循环中做的那样. 只要该对象可以实现__iter__方法,就可以进行迭代. 迭代对象调用__iter__方法会返 ...
- Python编写守护进程程序
Python编写守护进程程序思路 1. fork子进程,父进程退出通常,我们执行服务端程序的时候都会通过终端连接到服务器,成功连接后会加载shell环境,终端和shell都是进程,shell进程是终端 ...
- python自动化开发-[第三天]-编码,函数,文件操作
今日概要 - 编码详解 - 文件操作 - 初识函数 一.字符编码 1.代码执行过程 代码-->解释器翻译-->机器码-->执行 2.ASCII ASCII:一个Bytes代表一个字符 ...
- python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- 运维开发:python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- Django实现web端tailf日志文件
这是Django Channels系列文章的第二篇,以web端实现tailf的案例讲解Channels的具体使用以及跟Celery的结合 通过上一篇<Django使用Channels实现WebS ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- HDOJ的题目分类
模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 10 ...
- Sql 行转列、列转行及分页
说明:本实例是以 SQL Server 2005 为运行环境的. 准备工作:创建一个名为 DB 的数据库(CREATE DATABASE DB). 一.T-SQL 行转列 1.创建如下表 CREATE ...
- springmvc对jsonp的支持
在与前端开发人员合作过程中,经常遇到跨域名访问的问题,通常我们是通过jsonp调用方式来解决.jsop百科:http://baike.baidu.com/link?url=JKlwoETqx2uuKe ...
- SCGHR 分析思路
-- 分析某个模块业务 a:添加表,登记表,历史表,信息表 --- (把表名搞清楚,再看具体的字段) 先搞清楚大概的数据流向,在着手具体的数据,大处布局,小处着手 b:表中的字段,以及各表直接传递什么 ...
- windows 编程—— 宽字符集 与 Unicode
目录: 从ASCII码 到 Unicode Windows 编程中的 "字符” 定义 (如何在windows下进行通用编码) 常用的通用函数,定义 (本文为学习<Programming ...
- Hitting the 2100 parameter limit (SQL Server) when using Contains()
1down vote My solution (Guides -> List of Guid): List<tstTest> tsts = new List<tstTest&g ...
- 在Android项目中启用Java 8的部分特性--Lambda & Method References
Android N发布时同时发布了一个新的编译工具Jack(AS2.1+支持),基于Jack我们可以使用Java 8 的部分特性,在低版本机器上能使用的更少,同时Jack也有诸多不完善,工具链的改变难 ...
- ButterKnife的使用
ButterKnife是一个Android View注入的库. 1.开始使用 1.1 配置Eclipse 在使用ButterKnife需要先配置一下Eclipse. 项目右键-Properties-J ...
- 推荐一款不错的dialog小工具:artDialog
推荐一款不错的dialog小工具, 地址:http://www.planeart.cn/demo/artDialog/_doc/labs.html 相关介绍例如以下: artDialog是一个基于ja ...
- 怎样绕过oracle listener 监听的password设置
怎样绕过oracle 监听的password设置: 1.找到监听进程pid ,并将它kill 掉 ps -ef|grep tns [oracle@lixora admin]$ ps -ef|gr ...