我创建了一个修改的监视示例,以监视已添加到Windows中特定目录的.jpg照片的文件。

问题描述 投票:0回答:1
Modified: C:\images\C121211-0008.jpg Modified: C:\images\C121211-0009.jpg Modified: C:\images\C121211-0009.jpg <--- What? Modified: C:\images\C121211-0010.jpg Modified: C:\images\C121211-0011.jpg Modified: C:\images\C121211-0012.jpg Modified: C:\images\C121211-0013.jpg

我无法弄清楚为什么会发生这种情况!它似乎也不一致。如果有人能阐明这个问题,将不胜感激。
有类似的帖子,但它是针对Linux的:修改并创建了重复的事件

Python看门狗

过程写入文件时,首先创建它,然后一次写入内容。 

您看到的是一组与这些动作相对应的事件。 有时,这些作品很快写成,以至于Windows只为所有事件发送一个事件,而其他时候您会得到多个事件。

这是正常的...根据周围代码需要做什么,保留一个修改后的路径名而不是set

list
python python-2.7 pywin32 python-watchdog
1个回答
6
投票

在Windows上适用于我的用例的简单解决方法是检查文件的修改时间。这样的事情对我有用:

class MyHandler(FileSystemEventHandler): def __init__(self): self.times = {} def on_modified(self, event): try: t = os.path.getmtime(event.src_path) if event.src_path in self.times and t == self.times[event.src_path]: # duplicate event return self.times[event.src_path] = t except FileNotFoundError: # file got deleted after event was triggered try: del self.times[event.src_path] except KeyError: pass # continue processing event

如果您想从on_create()调用on_modify(),如果您想要一个回调以进行修改和创建,并且希望代码在OS上使用(是否有?

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.