python-使用pynotify读取新行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-使用pynotify读取新行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3332字,纯文字阅读大概需要5分钟。
内容图文
![python-使用pynotify读取新行](/upload/InfoBanner/zyjiaocheng/677/57a9c093539846b28a3c42a70a05c7e9.jpg)
我正在尝试显示添加到文件的新行.
因此,假设我有一个sample_file.txt:
1. line 1
2. line 2
3. line 3
我想检查此文件是否有新行,然后显示该行(无需再次打印所有文件)
#!/usr/bin/python
import os
import pyinotify
from datetime import datetime
import time
PATH = os.path.join(os.path.expanduser('~/'), 'sample_file.txt')
m = pyinotify.WatchManager()
m.add_watch(PATH, pyinotify.ALL_EVENTS, rec=True)
notifier = pyinotify.Notifier(m, pyinotify.ProcessEvent(), 0, 0, 100)
f = open(PATH)
for line in f.readlines():
print line
while True:
time.sleep(5)
try:
if notifier.check_events():
# RIGHT HERE:
# HOW TO DO SOMETHING LIKE
# f.last() ???
print f.next()
else:
print 'waiting for a line....'
except KeyboardInterrupt:
notifier.stop()
break
我当时想的是在while循环之前读取所有行,然后打印下一行,但是我的代码出了点问题,并且在循环之后立即检查f.next().
解决方法:
我将解决两个问题:
>如何在文件上实现拖尾,
>以及如何使用pyinotify模块.
尾巴在文件上
在您的代码中,您需要:
>尝试读取或读取尽可能多的完整行,
>将文件倒带到最后一个不完整的行的开头,直到可以使用搜索将其打印出来为止.
例如,这可以转换为:
f = open(PATH)
for line in f.readlines():
print line[:-1]
while True:
time.sleep(5)
try:
line_start = f.tell()
new_lines = f.read()
last_n = new_lines.rfind('\n')
if last_n >= 0:
# We got at least one full line
line_start += last_n + 1
print new_lines[:last_n]
else:
# No complete line yet
print 'no line'
f.seek(line_start)
except KeyboardInterrupt:
notifier.stop()
break
您可以在此处找到更多示例,尽管有些示例没有解决文件中未以换行符结尾的内容:
> https://github.com/kasun/python-tail/blob/master/tail.py
> http://code.activestate.com/recipes/157035-tail-f-in-python/
还有一些替代品How can I tail a log file in Python?
Pyinotify循环
您还应该按照文档中的说明将代码移动到pyinotify的事件处理程序中.
如果有要处理的事件,check_events将返回True,但实际上并不会处理该事件,因此,在处理事件之前,它将始终返回True.
另外,请尝试避免while / sleep循环. Inotify增加了在收到事件后立即处理事件的功能,而不会影响资源. while / sleep循环的反应性较小.
以下是在pyinotify上形成Short Tutorial的两种方法.
1.不断监控
如果没有其他事件循环,则这是首选方法,因为它是最活跃的:
PATH = os.path.join(os.path.expanduser('~/'), 'experiments', 'testfile')
class EventHandler(pyinotify.ProcessEvent):
def __init__(self, *args, **kwargs):
super(EventHandler, self).__init__(*args, **kwargs)
self.file = open(PATH)
self.position = 0
self.print_lines()
def process_IN_MODIFY(self, event):
print 'event received'
self.print_lines()
def print_lines(self):
new_lines = self.file.read()
last_n = new_lines.rfind('\n')
if last_n >= 0:
self.position += last_n + 1
print new_lines[:last_n]
else:
print 'no line'
self.file.seek(self.position)
wm = pyinotify.WatchManager()
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(PATH, pyinotify.IN_MODIFY, rec=True)
notifier.loop()
2.定期监测
如果您已经有一个处理循环,那么只需定期调用process_events即可. EventHandler类与方法1中的类相同,但是现在代替调用notifier.loop(),我们向notifier添加一个小的超时,并实现我们自己的事件循环.
...
wm = pyinotify.WatchManager()
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler, timeout=10)
wm.add_watch(PATH, pyinotify.IN_MODIFY, rec=True)
while True:
# Do something unrelated to pyinotify
time.sleep(5)
notifier.process_events()
#loop in case more events appear while we are processing
while notifier.check_events():
notifier.read_events()
notifier.process_events()
内容总结
以上是互联网集市为您收集整理的python-使用pynotify读取新行全部内容,希望文章能够帮你解决python-使用pynotify读取新行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。