【问题标题】:Monitoring a single file监控单个文件
【发布时间】:2013-04-28 09:21:30
【问题描述】:

我需要监控(使用watchdog)单个文件,而不是整个目录。

避免监控整个目录的最佳方法是什么? 我想this

class watchdog.events.PatternMatchingEventHandler(patterns=None, ignore_patterns=None, ignore_directories=False, case_sensitive=False)[source]

可能会有所帮助,但是如何为我的文件 (C:/dir1/dir2/file.txt) 定义合适的模式

【问题讨论】:

  • 抱歉跑题了,但我无法抗拒我的好奇心。为什么有人会自称汤姆克鲁斯? :)
  • s/dictionary/directory/g 也许?

标签: python watchdog


【解决方案1】:

如果您想查看像C:/dict1/dict2/file.txt 这样的文件路径,我认为这就是您的模式。里面没有通配符,所以它应该可以按原样使用。

顺便说一句,如果 Watchdog 给您带来麻烦,您也可以考虑使用 Pinotify:https://github.com/seb-m/pyinotify

【讨论】:

  • 是的,但是 C:/dict1/dict2/file.txt 作为一种模式不起作用......并且 pyinotify 仅适用于 Linux。
  • @TomCruise 您可能必须将其放入列表中,例如PatternMatchingEventHandler(patterns=['C:/dict1/dict2/file.txt'], ...)
  • 是的!它必须是一个列表!非常感谢!
【解决方案2】:

PatternMatchingEventHandler提供模式的方式是

 from watchdog.events import PatternMatchingEventHandler

 class MyHandler(PatternMatchingEventHandler):
     patterns = ["*.xml", "*.log", "*/test.txt"] # */test.txt to watch that specifi file

【讨论】:

  • Python 看门狗仍然在 Linux 中监视整个目录并在内部生成 inotify 事件,处理程序只是过滤掉您不想处理的内容。看门狗不是解决这个问题的方法。只需为您的项目激活调试日志记录,您就会看到。
  • 嗨@ravi404 和@Rene,如果我想忽略除“.csv”(我的patterns="*.csv")以外的所有文件格式,我应该在ignore_patterns 中输入什么?
猜你喜欢
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多