我遇到的问题是我看到了其他不期待的事件。
我正在观看文件夹C:\Users\kvasko\Downloads\data
。如果我复制文件夹2017\07\25\LogFile.xml
,我会看到以下3"创建"事件,当我希望只看到1.如果我提前创建日期文件夹结构(但是当应用程序运行时观看文件夹)它将只生成一个像我期望的事件。我从来没有获得仅创建文件夹的事件。就像为文件夹的创建生成事件一样,但是当检查我的on_created(self,event)
上发送的实际事件时,所有三个外观事件看起来完全相同。这是怎么回事?
以下是示例输出和最小示例。
2017-09-22 13:58:10,182 - root - INFO - Watchdog: file created C:\Users\kvasko\Downloads\data\2017\07\25\LogFile.xml
2017-09-22 13:58:11,184 - root - INFO - Watchdog: file created C:\Users\kvasko\Downloads\data\2017\07\25\LogFile.xml
2017-09-22 13:58:12,187 - root - INFO - Watchdog: file created C:\Users\kvasko\Downloads\data\2017\07\25\LogFile.xml
我希望:
2017-09-22 13:58:12,187 - root - INFO - Watchdog: file created C:\Users\kvasko\Downloads\data\2017\07\25\LogFile.xml
有没有办法检测文件夹创建中是否存在多个事件?
以下是我的观察者配置。
folder = "C:\\Users\\kvasko\\Downloads\\data"
observer = Observer(MyProcessHandler(patterns=["*.xml"]), folder, recursive=True)
observer.start_observer()
os.mkdirs("C:\\Users\\kvasko\\Downloads\\data\\2017\\07\\25")
shutil.copy2("C:\temp\LogFile.xml", "C:\\Users\\kvasko\\Downloads\\data\\2017\\07\\25")
try:
while True:
time.sleep(5)
except:
print("Error")
以下是我的处理程序类。
import logging
from watchdog.events import PatternMatchingEventHandler
class MyProcessHandler(PatternMatchingEventHandler):
def on_created(self, event):
logging.info("Watchdog: file created " + str(event.src_path))
编辑:
以下是最低工作示例:
import time
import os
import shutil
import datetime
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class TestEventHandler(PatternMatchingEventHandler):
def on_created(self, event):
print (str(datetime.datetime.now()) + " " + str(event))
if __name__ == '__main__':
path = "C:\\Temp"
event_handler = TestEventHandler(patterns=["*.xml"])
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
os.makedirs("C:\\Temp\\2017\\07\\25")
shutil.copy2("C:\\Temp2\\2017\\07\\25\\test.xml", "C:\\Temp\\2017\\07\\25")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
打印出来:
2017-09-22 15:49:51.334262 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
2017-09-22 15:49:52.335468 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
2017-09-22 15:49:53.340998 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
EDIT2:
将on_created()更改为on_any_event()。这就是生产的东西。
2017-09-23 13:14:57.288792 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
2017-09-23 13:14:58.291327 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
2017-09-23 13:14:59.293334 <FileCreatedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
2017-09-23 13:14:59.293334 <FileModifiedEvent: src_path='C:\\Temp\\2017\\07\\25\\test.xml'>
答案 0 :(得分:2)
您可能遇到this bug。作为解决方法,您可以使用TestEventHandler
类来记录创建的最后一个文件路径,而不是响应后续on_created
个事件
除非路径与上次创建的路径不同或者该路径已被删除:
import time
import os
import shutil
import datetime
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class TestEventHandler(PatternMatchingEventHandler):
def __init__(self, *args, **kwargs):
super(TestEventHandler, self).__init__(*args, **kwargs)
self.last_created = None
def on_created(self, event):
path = event.src_path
if path != self.last_created:
print(str(datetime.datetime.now()) + " " + str(event))
self.last_created = path
def on_deleted(self, event):
path = event.src_path
if path == self.last_created:
self.last_created = None
if __name__ == '__main__':
path = "C:\\Temp"
target_dir = "C:\\Temp\\2017\\07\\25"
src_dir = "C:\\Temp2\\2017\\07\\25"
filename = 'test.xml'
target = os.path.join(target_dir, filename)
src = os.path.join(src_dir, filename)
event_handler = TestEventHandler(patterns=["*.xml"])
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
if not os.path.exists(target_dir):
os.makedirs(target_dir)
if os.path.exists(target):
os.unlink(target)
for i in range(3):
shutil.copy2(src, target_dir)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()