回答編集履歴
3
修正(コードから日本語除去)
answer
CHANGED
@@ -37,18 +37,16 @@
|
|
37
37
|
import logging
|
38
38
|
import os
|
39
39
|
import datetime
|
40
|
-
import os
|
41
40
|
import time
|
41
|
+
import random
|
42
42
|
from watchdog.events import FileSystemEventHandler
|
43
43
|
from watchdog.observers import Observer
|
44
|
-
import random
|
45
44
|
|
46
|
-
|
45
|
+
folder_path = "監視対象のフォルダ"
|
47
46
|
|
48
|
-
logfiledir = "."
|
47
|
+
logfiledir = "."
|
49
|
-
|
50
48
|
sn = str.zfill(str(random.randint(0,10000)),4)
|
51
|
-
logfilename = datetime.datetime.now().strftime('%Y
|
49
|
+
logfilename = datetime.datetime.now().strftime('%Y%m%d_%H%M%S_'+sn) + "_log.txt"
|
52
50
|
|
53
51
|
logfilepath = os.path.join(logfiledir, logfilename)
|
54
52
|
logger = logging.getLogger(__name__)
|
@@ -65,39 +63,32 @@
|
|
65
63
|
logger.addHandler(handler2)
|
66
64
|
|
67
65
|
|
68
|
-
|
69
66
|
class ChangeHandler(FileSystemEventHandler):
|
70
67
|
|
71
68
|
def __init__(self):
|
72
69
|
super().__init__()
|
73
70
|
|
74
|
-
# ファイルやフォルダが作成された場合
|
75
71
|
def on_created(self, event):
|
76
72
|
if event.src_path != logfilepath:
|
77
|
-
logger.debug('on_created() : %s
|
73
|
+
logger.debug('on_created() : %s'%(event.src_path))
|
78
74
|
|
79
|
-
# ファイルやフォルダが更新された場合
|
80
75
|
def on_modified(self, event):
|
81
76
|
if event.src_path != logfilepath:
|
82
|
-
logger.debug(f"on_modified(): {event.src_path}
|
77
|
+
logger.debug(f"on_modified(): {event.src_path}")
|
83
78
|
|
84
|
-
# ファイルやフォルダが移動された場合
|
85
79
|
def on_moved(self, event):
|
86
|
-
logger.debug('on_moved() : %s
|
80
|
+
logger.debug('on_moved() : %s ' % event.src_path)
|
87
81
|
|
88
|
-
# ファイルやフォルダが削除された場合
|
89
82
|
def on_deleted(self, event):
|
90
|
-
logger.debug('on_deleted() : %s
|
83
|
+
logger.debug('on_deleted() : %s ' % event.src_path)
|
91
84
|
|
92
85
|
event_handler = ChangeHandler()
|
93
86
|
observer = Observer()
|
94
87
|
|
95
|
-
# フォルダの監視
|
96
|
-
observer.schedule(event_handler,
|
88
|
+
observer.schedule(event_handler, folder_path, recursive=False)
|
97
89
|
|
98
|
-
# 監視の開始
|
99
90
|
observer.start()
|
100
|
-
logger.debug(f"
|
91
|
+
logger.debug(f"Started to observe. [path : {logfilepath}]")
|
101
92
|
print()
|
102
93
|
try:
|
103
94
|
while observer.is_alive():
|
@@ -105,12 +96,7 @@
|
|
105
96
|
except KeyboardInterrupt:
|
106
97
|
pass
|
107
98
|
|
108
|
-
|
109
|
-
# 監視の終了
|
110
99
|
observer.stop()
|
111
|
-
# スレッド停止を待つ
|
112
100
|
observer.join()
|
113
|
-
# 終了
|
114
|
-
logger.debug('
|
101
|
+
logger.debug('Script quit.')
|
115
|
-
|
116
102
|
```
|
2
random追加
answer
CHANGED
@@ -41,8 +41,8 @@
|
|
41
41
|
import time
|
42
42
|
from watchdog.events import FileSystemEventHandler
|
43
43
|
from watchdog.observers import Observer
|
44
|
+
import random
|
44
45
|
|
45
|
-
|
46
46
|
target_dir = "監視対象のフォルダ"
|
47
47
|
|
48
48
|
logfiledir = "." # ログファイルを保存するフォルダ
|
1
answer
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
・watchlogが同じファイルの作成を二重検知している可能性
|
5
5
|
・ファイル送信csvは一度しか配置されないはずだが、内部で別の隠しファイルが一時的に作成され、それを検知している可能性
|
6
6
|
|
7
|
-
まず上記の切り分けのために、下記コードを、CSVメール・ファイル退避の代わりに起動していただけないでしょうか。
|
7
|
+
まず上記の切り分けのために、下記コードを、**CSVメール・ファイル退避用コードの代わりに**起動するようにバッチファイルに記述していただけないでしょうか。
|
8
8
|
|
9
9
|
|
10
10
|
単純に、下のコードの起動以降の、監視対象フォルダ内のファイルの動きを、ログファイルとして表示・記録するものです。
|