[pyinotify] help required to send only one mail for chunk of events

Chris Angelico rosuav at gmail.com
Mon May 23 20:09:36 EDT 2016


On Tue, May 24, 2016 at 6:20 AM, Mohan L <l.mohanphy at gmail.com> wrote:
> I wanted to send an email notification with content of logfile after 15
> mins on any change. The idea is I want to send only one mail for chunk of
> events.Say for example, I want to send only one email when some one extract
> a tar.gz file in watched directory.

I'm ignoring your actual code, because the formatting is all messed up
- your mail/news client appears to have destroyed your indentation.
But there are two straight-forward ways of doing what you want here:

1) Whenever there's a change, wait fifteen minutes, then send an email
with all the recent changes.

2) Whenever there's a change, check to see when you last sent an
email, and if it's more than fifteen minutes ago, send one
immediately.

I'm not entirely sure how to interpret your "after 15 mins", but my
guess is that it's one of the above.

Oh, I lied. I am looking at your code, just a bit.

> try:
> import pyinotify
> except:
> print "pyinotify not installed"
> sys.exit(1)

Don't do this. Ever. If you want to use pyinotify, there's one thing to do:

import pyinotify

If that doesn't work, Python will print an error message and then exit
1, so having code like this is at best just duplicating what already
exists. The default message is far more helpful, though, so you're
destroying information - a lot of it. You use a bare 'except:' further
down, too; again, don't. Figure out _exactly_ what exception you're
intending to catch, and catch that.

Finally: Consider upgrading to Python 3. With a script this size, I
doubt there's anything holding you on Python 2 (the two primary
dependencies are pyinotify and the sending of emails, both of which
are available on Py3), so this would be well worth porting.

ChrisA



More information about the Python-list mailing list