这是模块:
from pyinotify import WatchManager,
ThreadedNotifier, ProcessEvent, IN_CREATE
class Watcher(ProcessEvent):
watchdir = '/tmp/watch'
def __init__(self):
ProcessEvent.__init__(self)
wm = WatchManager()
self.notifier = ThreadedNotifier(wm, self)
wdd = wm.add_watch(self.watchdir, IN_CREATE)
self.notifier.start()
def process_IN_CREATE(self, event):
pfile = self._parse(event.pathname)
print(pfile)
def _parse(self, filename):
f = open(filename)
file = [line.strip() for line in f.readlines()]
f.close()
return file
if __name__ == '__main__':
Watcher()
问题是_parse返回的列表在被新文件创建事件触发时是空的,就像这样(当watcher.py运行时,文件在另一个窗口中创建):
$python watcher.py
[]
…但奇怪的是,它直接调用时从解释器会话开始工作.
>>> import watcher
>>> w = watcher.Watcher()
>>> w._parse('/tmp/watch/sample')
['This is a sample file', 'Another line', 'And another...']
为什么会这样?我调试这个东西的最远的地方就是要知道有些东西正在使pyinotify无法正确读取文件.但为什么?
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-zaichuangjianshiduquwenjiandepyinotifycuowu.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 13, 2019 at 02:28 上午