0

Preface: This is my first post on Stack Overflow, so forgive me if at any point I am not following the proper conventions on how to properly pose questions to the community/link to other stack overflow articles where I have received direction.

A project at work was assigned to me where there is a need for a directory to be monitored for any changes and upon the event that an action occurs, an email is generated and sent to the appropriate parties who manage the directory.

Utilizing Tim Golden's Python Stuff: Watch a Directory for Changes (with slight modifications) and a post entitled How to Get the Last Modified Date of a File in Python I have been able to utilize Python to cobble together some code that continuously monitors the directory and is successful at returning the actions that occurred with a corresponding time stamp of when the directory was last modified (the timestamp is not refined enough to be so granular that it captures the exact time of each action, but rather attributes the time of the last action in the folder to all actions—which is not ideal).

I am stuck on next steps for how to go about handling the triggering of an automatic email notification (utilizing MS outlook) when any action takes place.

I have been reading through the site and see possible solutions namely: Email notification on file change in particular directory with Python however I was hoping for a solution that could work with my existing code.

Any insights, suggestions or guidance would be greatly appreciated. Thanks in advance.

Below is what I have so far:

 import os

    import win32file
    import win32con

    from datetime import datetime

    lastmodified= os.stat("generic path of directory").st_mtime

    timestamp = (datetime.fromtimestamp(lastmodified))

    ACTIONS = {
      1 : "FILE_ACTION_ADDED",
      2 : "FILE_ACTION_REMOVED",
      3 : "FILE_ACTION_MODIFIED",
      4 : "FILE_ACTION_RENAMED_OLD_NAME",
      5 : "FILE_ACTION_RENAMED_NEW_NAME",
      8 : "FILE_NOTIFY_CHANGE_SIZE",
      10 : "FILE_NOTIFY_CHANGE_LAST_WRITE",
      20 : "FILE_NOTIFY_CHANGE_CREATION",
      40 : "FILE_NOTIFY_CHANGE_CREATION",
      100 : "FILE_NOTIFY_CHANGE_SECURITY"
    }
    # Thanks to Claudio Grondi for the correct set of numbers
    FILE_LIST_DIRECTORY = 0x0001

    path_to_watch = "generic path of directory"
    hDir = win32file.CreateFile (
      path_to_watch,
      FILE_LIST_DIRECTORY,
      win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
      None,
      win32con.OPEN_EXISTING,
      win32con.FILE_FLAG_BACKUP_SEMANTICS,
      None
    )
    while 1:
      #
      # ReadDirectoryChangesW takes a previously-created
      # handle to a directory, a buffer size for results,
      # a flag to indicate whether to watch subtrees and
      # a filter of what changes to notify.
      #
      # NB Tim Juchcinski reports that he needed to up
      # the buffer size to be sure of picking up all
      # events when a large number of files were
      # deleted at once.
      #
      results = win32file.ReadDirectoryChangesW (
        hDir,
        1024,
        True,
        win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
         win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
         win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
         win32con.FILE_NOTIFY_CHANGE_SIZE |
         win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
         win32con.FILE_NOTIFY_CHANGE_SECURITY,
        None,
        None
    )
      for action, file in results:
        full_filename = os.path.join (path_to_watch, file)
        print (full_filename, ACTIONS.get (action, "Unknown"),timestamp)
  • you would probably use smtplib and combine it with an smtp server (gmail provides one) ... I do not think you should use "outlook" for this that will make things much more complicated – Joran Beasley Feb 05 '20 at 04:27
  • Joran, Thanks for being the first to respond. I have seen a solution utilizing smtplib in the “Sending Emails” section Automate the Boring Stuff with Python however my company is locked down when it comes to utilizing gmail; I strongly suspect that the parties that would be receiving the email would be more comfortable with solution that results in an email that originates from inside of the organization. But taking your suggestion I did some more research into the library as a possible solution – Dev the Funky Duvalian Feb 05 '20 at 07:35
  • Using a work-issued computer with python 2.7.15, this is the approved version available enterprise wide. I am in the process of submitting a request for a later version, but this will be a protracted approval process—as such I have to go the PIP install route; and that is where I am receiving the “there was a problem confirming the ssl certificate” error message. The “pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org ” solution is not solving the issue; this may be due to a corporate firewall, but I am not savvy enough to determine if that is the issue. – Dev the Funky Duvalian Feb 05 '20 at 07:40
  • smtplib is builtin ... if thats the case(unable to use gmail) they should have their own smtp server that they can connect to instead of gmail ... smtp is most likely what they use to wire up msoutlook to the actual email server (it might be pop3 ... but then you can use poplib or whatever ... (also builtin)) – Joran Beasley Feb 05 '20 at 07:44
  • Thanks for the direction on checking to see how they wire up ms outlook. The reason that I mentioned being limited to 2.7.15 is that I thought I read somewhere that it came built in in versions later than mine. I guess I was mistaken. Appreciate the heads up. – Dev the Funky Duvalian Feb 07 '20 at 06:00

0 Answers0