python tail是gnu tail和head的简单实现。
tailer的Python项目详细描述
Python tail是GNU tail和head的简单实现
它提供了3个主要函数,可以在任何支持seek()和tell()的类文件对象上执行。
- tail-从文件末尾读取行
- head-从文件顶部读取行
- follow-随着文件的增长读取行
它还附带了pytail,这是一个命令行版本,提供与GNU tail相同的功能这在没有尾部等价物的windows系统上特别有用。
安装
使用pip或easy_install安装。
pip install tailer
示例
import tailer f = open('test.txt', 'w') for i in range(11): f.write('Line %d\\n' % (i + 1)) f.close()
尾部
# Get the last 3 lines of the file tailer.tail(open('test.txt'), 3) # ['Line 9', 'Line 10', 'Line 11']
头部
# Get the first 3 lines of the file tailer.head(open('test.txt'), 3) # ['Line 1', 'Line 2', 'Line 3']
跟随
# Follow the file as it grows for line in tailer.follow(open('test.txt')): print line
运行测试
泰勒目前只有博士学位。
用鼻子进行测试:
nosetests --with-doctest src/tailer
使用doctest运行测试:
python -m doctest -v src/tailer/__init__.py