You can watch the folder then act on the files creation and/or modify events. Your script can be watching a remote drive on the network if needed.
The following is cut out of working code but I can't confirm it works in this snippet state. It'll call HandleNewlyCreated() for newly created files.
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class WatchPendingHandler(FileSystemEventHandler):
''' Run a handler for every file added to the pending dir
This class also handles what I call a bug in the watchdog module
which means that you can get more than one call per real event
in the watched dir tree.
'''
def __init__(self):
super(WatchPendingHandler, self).__init__()
# wip is used to avoid bug in watchdog which means multiple calls
# for one real event.
# For reference: https://github.com/gorakhargosh/watchdog/issues/346
self.wip = []
def on_created(self, event):
path = event.src_path
if event.is_directory:
logging.debug('WatchPendingHandler() New dir created in pending dir: {}'.format(path))
return
if path in self.wip:
logging.debug('WatchPendingHandler() Dup created event for %s', path)
return
self.wip.append(path)
logging.debug('WatchPendingHandler() New file created in pending dir: {}'.format(path))
HandleNewlyCreated(path)
def on_moved(self, event):
logging.debug('WatchPendingHandler() %s has been moved', event.src_path)
with contextlib.suppress(ValueError):
self.wip.remove(event.src_path)
def on_deleted(self, event):
path = event.src_path
logging.debug('WatchPendingHandler() %s has been deleted', path)
with contextlib.suppress(ValueError):
self.wip.remove(path)
observer = Observer()
observer.schedule(WatchPendingHandler(), DIR_PENDING, recursive=True)
observer.start()
logging.info('Watching %s', DIR_PENDING)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Explanation of on_created():
It will check if the event was for the creation of a directory. If so it logs the event and then returns/ignores it. It then checks whether the file created event has previously happened (path in self.wip). If so, it also ignores the event and returns. Now it can record the event in self.wip, log the new event and then call HandleNewlyCreated() to process the newly created file.
If the file is moved or deleted, then we need to remove the path from self.wip so that the next created event for a new file won't be ignored.
More info here: https://pypi.org/project/watchdog/