瀏覽代碼

ignore files having a handle

test_without_lockfile
Dirk Alders 1 年之前
父節點
當前提交
faae45b030
共有 1 個文件被更改,包括 14 次插入1 次删除
  1. 14
    1
      file2mail.py

+ 14
- 1
file2mail.py 查看文件

@@ -6,6 +6,7 @@ import json
6 6
 import logging
7 7
 import mail
8 8
 import os
9
+import psutil
9 10
 import report
10 11
 import time
11 12
 
@@ -16,6 +17,18 @@ logger = logging.getLogger('root')
16 17
 report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
17 18
 
18 19
 
20
+def has_handle(fpath):
21
+    for proc in psutil.process_iter():
22
+        try:
23
+            for item in proc.open_files():
24
+                if fpath == item.path:
25
+                    return True
26
+        except Exception:
27
+            pass
28
+
29
+    return False
30
+
31
+
19 32
 if __name__ == "__main__":
20 33
     while True:
21 34
         # Check if LOCK_FILE EXISTS
@@ -32,7 +45,7 @@ if __name__ == "__main__":
32 45
                 # Scan for files to send
33 46
                 for filename in fstools.filelist(config.TARGET_FOLDER):
34 47
                     # Exclude LOCK_FILE
35
-                    if filename != LOCK_FILE:
48
+                    if filename != LOCK_FILE and not has_handle(filename):
36 49
                         logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
37 50
                         # Send File as E-Mail
38 51
                         try:

Loading…
取消
儲存