【问题标题】:pyinotify: Handling IN_MODIFY triggerspyinotify:处理 IN_MODIFY 触发器
【发布时间】:2010-07-15 17:06:55
【问题描述】:

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

我不想处理这么多触发器。在复制文件后,我只想触发一个事件。我如何做到这一点?

任何 Pyinotify 大师都可以提供帮助

【问题讨论】:

  • 你应该检查this issue in pyinotify。 IN_MODIFY 事件受更改写入文件方式的影响很大(例如,nano 触发 2 个 IN_MODIFY 事件,而 sublime-text 不会触发任何事件)

标签: python file inotify pyinotify


【解决方案1】:

尝试将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()

【讨论】:

  • @Ajoy:感谢您的通知。链接已修复,上面的代码重复了。
猜你喜欢
  • 2014-02-02
  • 2013-11-23
  • 2015-11-29
  • 2021-04-25
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多