Skip to content
Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env python
"""$ python /exam/ple .txt,.html /pri/vate
Answer for
Adopted from [1] example.
- easier to install than ``inocron`` due to pyinotify is pure Python
- event-driven -- less impact than the perl script
Linux, Python
import os, shutil, subprocess, sys
import pyinotify
from pyinotify import log
class Handler(pyinotify.ProcessEvent):
def my_init(self, **kwargs):
def process_IN_CLOSE_WRITE(self, event):
# file was closed, ready to move it
if event.dir or os.path.splitext([1] not in extensions:
# directory or file with uninteresting extension
return # do nothing
log.debug('==> moving %s' %
shutil.move(event.pathname, os.path.join(self.destdir,
cmd = self.cmd + []
log.debug("==> calling %s in %s" % (cmd, self.destdir)), cwd=self.destdir)
except (IOError, OSError, shutil.Error), e:
def process_default(self, event):
def mainloop(path, handler):
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, default_proc_fun=handler)
wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
log.debug('==> Start monitoring %s (type c^c to exit)' % path)
if __name__ == '__main__':
if len(sys.argv) < 5:
print >> sys.stderr, "USAGE: %s dir ext[,ext].. destdir cmd [args].." % (
path = sys.argv[1] # dir to monitor
extensions = set(sys.argv[2].split(','))
destdir = sys.argv[3]
cmd = sys.argv[4:]
log.setLevel(10) # verbose
# Blocks monitoring
mainloop(path, Handler(path=path, destdir=destdir, cmd=cmd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.