2013-04-28 41 views
3

我需要監控(使用watchdog)單個文件,而不是整個目錄。監控單個文件

避免監視整個目錄的最佳方法是什麼? 我想this

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

可能是有用的,但如何定義一個適當的方式爲我的文件(C:/dir1/dir2/file.txt)

+1

對不起,我是題外話,但我可以」不要拒絕我的好奇心。爲什麼有人會自稱湯姆克魯斯? :) – 2013-04-28 09:24:31

+3

's/dictionary/directory/g'也許? – Aya 2013-04-28 09:24:47

回答

2

如果你想觀看像C:/dict1/dict2/file.txt這樣的文件路徑,我認爲那就是你的模式。沒有通配符,所以它應該原樣使用。

順便說一句,如果看門狗給你的麻煩,你也可以考慮pyinotify中:https://github.com/seb-m/pyinotify

+0

是的,但'C:/ dict1/dict2/file.txt'作爲模式不起作用...... pyinotify僅適用於Linux。 – 2013-04-28 09:38:11

+1

@TomCruise您可能必須將其放入列表中,例如'PatternMatchingEventHandler(patterns = ['C:/dict1/dict2/file.txt'],...)' – Aya 2013-04-28 09:54:08

+0

YES!它必須是一個列表!非常感謝! – 2013-04-28 10:07:25

1

PatternMatchingEventHandler提供模式的方式是

from watchdog.events import PatternMatchingEventHandler 

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