pyinotify:在修改触发器中处理

2024-10-01 10:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图观察一个目录,并正在寻找文件修改。正在考虑使用pyinotify。问题是,当使用IN_MODIFY事件检查文件更改时,如果我通过网络将一个小文件(比如12 MB)复制到目录中,它会触发相当多的事件。在

我不想处理这么多触发器。我只想在复制文件后触发一个事件。我怎样才能做到呢?在

任何灵媒大师都能帮上忙


Tags: 文件in网络目录事件mb触发器modify
1条回答
网友
1楼 · 发布于 2024-10-01 10:17:30

尝试将IN_MODIFY更改为IN_CLOSE_WRITE。 关闭可写文件时,IN_CLOSE_WRITE事件发生。这应该只发生一次,除非复制文件的程序选择多次关闭该文件。在

上面的更改可能是您需要的全部内容,但是如果不是的话,this basic code 可以是一个非常有用的工具来查看事件发生的时间。有了它,您应该能够确定要使用哪个事件。在


# Example: loops monitoring events forever.
#
import pyinotify

# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and
# process events).
notifier = pyinotify.Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
# Loop forever and handle events.
notifier.loop()

相关问题 更多 >