Pyinotify error with reading file when creating?

I want to parse a file every time a new file is created in a specific directory. For this, I'm trying to use pyinotify to configure the directory to view kernel events IN_CREATEand run parse().

Here is the module:

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()

The problem is that the list returned by _parse is empty when launched using a new file creation event (for example, the file is created in another window, but it watcher.pyworks):

$ python watcher.py
[]

... but oddly enough, it works from an interpreter session when called directly.

>>> import watcher
>>> w = watcher.Watcher()
>>> w._parse('/tmp/watch/sample')
['This is a sample file', 'Another line', 'And another...']

Why is this happening? The farthest thing I came to debug this thing is to know that something is causing pyinotify to not read the file correctly. But why?

+3
4

, , ?

+3

@SilentGhost, , - (.. , ).

. loop.py pynotify tarball inotify. , , loop.py /tmp, , .

+1

, , 2.6.18, Python 2.4.3 pyinotify 0.7.1 - , , , ...:

#!/usr/bin/python2.4

import os.path
from pyinotify import pyinotify

class Watcher(pyinotify.ProcessEvent):

    watchdir = '/tmp/watch'

    def __init__(self):
        pyinotify.ProcessEvent.__init__(self)
        wm = pyinotify.WatchManager()
        self.notifier = pyinotify.ThreadedNotifier(wm, self)
        wdd = wm.add_watch(self.watchdir, pyinotify.EventsCodes.IN_CREATE)
        print "Watching", self.watchdir
        self.notifier.start()

    def process_IN_CREATE(self, event):
        print "Seen:", event
        pathname = os.path.join(event.path, event.name)
        pfile = self._parse(pathname)
        print(pfile)

    def _parse(self, filename):
        f = open(filename)
        file = [line.strip() for line in f]
        f.close()
        return file

if __name__ == '__main__':
      Watcher()

,

echo "ciao" >/tmp/watch/c3

:

Watching /tmp/watch
Seen: event_name: IN_CREATE   is_dir: False   mask: 256   name: c3   path: /tmp/watch   wd: 1   
['ciao']

. , script ( Python hashbang, , ) Linux, pyinotify Python, , circunstances? , , , . !

+1

, , IN_CLOSE_WRITE. , , .

@Alex: , script, : Python 2.6.1, pyinotify 0.8.6 Linux 2.6.28, .

It was definitely trying to parse the file before it was written, so SilentGhost and DanM came in handy to figure this out.

+1
source

Source: https://habr.com/ru/post/1707149/


All Articles