From e2669dd265d871a7167aa26b5bc33e1ccc4b9e6a Mon Sep 17 00:00:00 2001 From: ahosking Date: Tue, 30 May 2023 13:44:26 -0400 Subject: [PATCH] Add Scans Watch a given directory for changes and print out the file list difference --- scans/scans.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 scans/scans.py diff --git a/scans/scans.py b/scans/scans.py new file mode 100644 index 0000000..8df58de --- /dev/null +++ b/scans/scans.py @@ -0,0 +1,42 @@ +from os import listdir +from os.path import isfile, join +import time + +watch_directory = '/Users/alexanderh/watch_folder' +pollTime = 10 + + +def list_files(path): + return [f for f in listdir(path) if isfile(join(path, f))] + + +def listComparison(OriginalList: list, NewList: list): + differencesList = [x for x in NewList if x not in OriginalList] + return differencesList + + +def fileWatcher(watch_directory: str, pollTime: int): + while True: + if 'watching' not in locals(): + previousFileList = list_files(watch_directory) + watching = True + print('First Time') + print(previousFileList) + + time.sleep(pollTime) + + newFileList = list_files(watch_directory) + + fileDiff = listComparison(previousFileList, newFileList) + previousFileList = newFileList + if len(fileDiff) == 0: + continue + notify_new_files(fileDiff) + + +def notify_new_files(fileDiff: list): + print(fileDiff) + + +# print(list_files('/Users/alexanderh/ahosking_git/python-tools')) +fileWatcher(watch_directory, pollTime)