python实现tail -f命令功能
来源:互联网 发布:淘宝店运营视频教程 编辑:程序博客网 时间:2024/05/01 06:20
#!/usr/bin/env python#!encoding:utf-8'''Python-Tail - Unix tail follow implementation in Python.python-tail can be used to monitor changes to a file.Example: import tail # Create a tail instance t = tail.Tail('file-to-be-followed') # Register a callback function to be called when a new line is found in the followed file. # If no callback function is registerd, new lines would be printed to standard out. t.register_callback(callback_function) # Follow the file with 5 seconds as sleep time between iterations. # If sleep time is not provided 1 second is used as the default time. t.follow(s=5) '''# Author - Kasun Herath <kasunh01 at gmail.com># Source - https://github.com/kasun/python-tailimport osimport sysimport timeclass Tail(object): ''' Represents a tail command. ''' def __init__(self, tailed_file): ''' Initiate a Tail instance. Check for file validity, assigns callback function to standard out. Arguments: tailed_file - File to be followed. ''' self.check_file_validity(tailed_file) self.tailed_file = tailed_file self.callback = sys.stdout.write def follow(self, s=1): ''' Do a tail follow. If a callback function is registered it is called with every new line. Else printed to standard out. Arguments: s - Number of seconds to wait between each iteration; Defaults to 1. ''' with open(self.tailed_file) as file_: # Go to the end of file file_.seek(0,2) while True: curr_position = file_.tell() line = file_.readline() if not line: file_.seek(curr_position) else: self.callback(line) time.sleep(s) def register_callback(self, func): ''' Overrides default callback function to provided function. ''' self.callback = func def check_file_validity(self, file_): ''' Check whether the a given file exists, readable and is a file ''' if not os.access(file_, os.F_OK): raise TailError("File '%s' does not exist" % (file_)) if not os.access(file_, os.R_OK): raise TailError("File '%s' not readable" % (file_)) if os.path.isdir(file_): raise TailError("File '%s' is a directory" % (file_))class TailError(Exception): def __init__(self, msg): self.message = msg def __str__(self): return self.messageif __name__=='__main__':args = sys.argvif len(args)<2:print 'need one filename parameter'exit(1)t = Tail(args[1])#t.register_callback(print_line)t.follow()
1 0
- python实现tail -f命令功能
- Python自定制tail命令,tail -f 功能小解
- python实现tail命令,包含-f -n参数
- Java实现linux的“tail -f”命令
- 如何实现Linux下高亮关键字的tail -f功能
- 如何实现Linux下高亮关键字的tail -f功能
- [twitter] 如何实现 tail - f
- DOS下类似LINUX下的tail -f 功能的命令
- tail -f run.log 功能解释
- linux下c语言实现tail -f功能---实时读取变化文件中的增量内容
- linux下c语言实现tail -f功能---实时读取变化文件中的增量内容
- linux nohup和tail-f命令用法
- tail命令实现
- 用Python实现一个类Unix的tail命令
- Tail for php,php实现tail命令
- tail -f
- tail日志跟踪功能实现
- tail -f -F区别
- 随笔
- const int *p和int * const p的区别(常量指针与指向常量的指针)
- HDOJ题目2502月之数(数学,递推)
- Oracle体系结构
- 资源汇总
- python实现tail -f命令功能
- HDU4941Magical Forest(map)
- hbase scan 类
- 6.2.8 In Action
- hdu-oj 1219 AC Me
- 记录一个程序中的小bug,关于GUI的JTable组件
- Stockbroker Grapevine
- hdu 1051(贪心算法)
- hbase filter类