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)