forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix_tail.py
More file actions
41 lines (30 loc) · 725 Bytes
/
Copy pathunix_tail.py
File metadata and controls
41 lines (30 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic:
利用generator模仿tail -f www.log | grep "python"
对变化的日志文件持续查看含有python的行
Desc :
"""
import time
__author__ = 'Xiong Neng'
def tail(f):
f.seek(0, 2) # 移动到EOF
while True:
line = f.readline()
if not line:
time.sleep(0.2)
continue
yield line
def grep(lines, search_text):
for line in lines:
if search_text in line: yield line
def my_tail_search():
wwwlog = tail(open("www.log"))
pylines = grep(wwwlog, "python")
for line in pylines:
print(line)
def main():
pass
if __name__ == '__main__':
main()