From faae45b030574c92da79a421cd0c7e31992c6ec0 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 10 Jun 2023 20:25:59 +0200 Subject: [PATCH] ignore files having a handle --- file2mail.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/file2mail.py b/file2mail.py index b23f2f9..ac4b152 100644 --- a/file2mail.py +++ b/file2mail.py @@ -6,6 +6,7 @@ import json import logging import mail import os +import psutil import report import time @@ -16,6 +17,18 @@ logger = logging.getLogger('root') report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ]) +def has_handle(fpath): + for proc in psutil.process_iter(): + try: + for item in proc.open_files(): + if fpath == item.path: + return True + except Exception: + pass + + return False + + if __name__ == "__main__": while True: # Check if LOCK_FILE EXISTS @@ -32,7 +45,7 @@ if __name__ == "__main__": # Scan for files to send for filename in fstools.filelist(config.TARGET_FOLDER): # Exclude LOCK_FILE - if filename != LOCK_FILE: + if filename != LOCK_FILE and not has_handle(filename): logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO) # Send File as E-Mail try: