实现Python版的tail -f功能

tail -f 的功能非常好用。我们用Python也可以实现这样的功能。
实现的原理是通过Python版本的inotify获得文件的更新消息,从而读取更新的行。pyinotify的下载地址https://github.com/seb-m/pyinotify下载解压后得到如下文件

#ls
ACKS build common COPYING dist MANIFEST.in old python2 python3 README.md setup.py

执行如下命令进行安装。

python setup.py build
python setup.py install

然后就可以使用Python版本的inotify功能了。
下面是我写的一个简单的tail -f文件的实现。

#!/usr/bin/python

import pyinotify
import time
import os
class ProcessTransientFile(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
line = file.readline()
if line:
print line, # already has newline filename = '/tmp/test1234'
file = open(filename,'r')
#Find the size of the file and move to the end
st_results = os.stat(filename)
st_size = st_results[6]
file.seek(st_size) wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.watch_transient_file(filename, pyinotify.IN_MODIFY, ProcessTransientFile)
notifier.loop()

tail的文件为/tmp/test1234,通过向/tmp/test1234写入内容,可以看到python脚本可以动态显示更新的内容。
这个小脚本只是抛砖引玉。通过监听文件,尤其是日志文件可以实现很多诸如报警、自动控制等功能。

 
本文转自 “http://www.opstool.com/article/226”

Pytho实现tail -f的更多相关文章

  1. PHP实现linux命令tail -f

    PHP实现linux命令tail -f 今天突然想到之前有人问过我的一个问题,如何通过PHP实现linux中的命令tail -f,这里就来分析实现下. 这个想一想也挺简单,通过一个循环检测文件,看文件 ...

  2. tail -f 和 -F 的用法

    tail -f 和 -F 的用法  Tai 2010-08-16 16:03:18 -f 是--follow[=HOW]的缩写, 可以一直读文件末尾的字符并打印出来."[=HOW]" ...

  3. tail -f logfile.log 一直监控某个文件,若该文件有改动,立即在屏幕上输出

    tail -f logfile.log 可以一直监控某个文件,只要文件有改动,就立即在屏幕上输出

  4. linux工具问题,tail -f 失效

    最近发现一个很奇怪问题: tail -f 不能实时的输出日志

  5. python10min系列之面试题解析:python实现tail -f功能

    同步发布在github上,跪求star 这篇文章最初是因为reboot的群里,有人去面试,笔试题有这个题,不知道怎么做,什么思路,就发群里大家讨论 我想了一下,简单说一下我的想法吧,当然,也有很好用的 ...

  6. JAVA 实现tail -f 日志文件监控功能

    工具: <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</ar ...

  7. tail -f 报错 file truncated

    操作: 循环覆盖向tmp 文件写入坐标 tmp: -45.6976089525,-26.1528715421,-0.0188627654187 报错如下: -15.2517398838,-5.1216 ...

  8. tail -f 命令暂停方法

    Linux 下查看日志时,使用 tail -f 可以不断的刷新日志信息. 例如: tail -f logs.log 此时要想暂停刷新,使用ctrl+s暂停终端.若想继续终端,使用ctrl+q. 若想退 ...

  9. tailf、tail -f、tail -F三者区别(转)

    tail -f    等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止 tail -F     等同于--follow=name  --retry,根 ...

随机推荐

  1. DS实验题 sights

    算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...

  2. Nginx平滑重启

    kill -HUP cat /usr/local/nginx/logs/nginx.pid`

  3. DropzoneJS 使用指南

    原文链接http://segmentfault.com/a/1190000004045240 官方文档: http://www.dropzonejs.com/ Github: https://gith ...

  4. Gradient

    https://en.wikipedia.org/wiki/Gradient

  5. DELPHI2007 安装ACTIVEX插件的方法

    先新建一个Package    file----NEW-----Package Delphi for win32, 再在Component->Import Component里面添加好Activ ...

  6. Python之list添加新元素、删除元素、替换元素

    Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...

  7. 蓝牙Bluetooth技术手册规范下载

    [背景] 之前就已经整理和转帖了和蓝牙技术相关的一些内容: [资源下载]bluetooth 协议 spec specification 蓝牙1.1.蓝牙1.2.蓝牙2.0(蓝牙2.0+EDR)区别 但 ...

  8. linux shell send and receive emails

    http://www.netcan666.com/2016/02/20/%E5%88%A9%E7%94%A8telnet%E5%9C%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E5% ...

  9. FW Docker为容器分配指定物理网段的静态IP

    官方有关于网桥和IP配置的文档地址:https://docs.docker.com/articles/networking/ 1.宿主机(系统采用ubuntu-14.04.1-server-amd64 ...

  10. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...