我正在运行以下代码,在Python中实现WatchDog以监视文件更改。我试图跟踪日志文件(DoNotDelete.txt)中的所有更改,因此我试图在我的代码中忽略此DoNotDelete.txt文件。但是当我尝试运行它时(首先通过将'n'传递给文件然后通过更改文件),我得到一个ValueError(ValueError:包含和排除冲突模式set(['*'])
)。
import sys
import time
import os
import logging
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
currentEvent = ""
update = False
def on_modified(self, event):
super(MyHandler, self).on_modified(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", modified, " + event.src_path
if(self.update == False):
file = open("DoNotDelete.txt", "a+")
file.write(self.currentEvent + "\n")
file.close()
else:
print self.currentEvent
def on_created(self, event):
super(MyHandler, self).on_created(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", created, " + event.src_path
if(self.update == False):
file = open("DoNotDelete.txt", "a+")
file.write(self.currentEvent + "\n")
file.close()
else:
print self.currentEvent
def on_deleted(self, event):
super(MyHandler, self).on_deleted(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", deleted, " + event.src_path
if(self.update == False):
file = open("DoNotDelete.txt", "a+")
file.write(self.currentEvent + "\n")
file.close()
else:
print self.currentEvent
def on_moved(self, event):
super(MyHandler, self).on_moved(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", moved, from: " + event.src_path + ", to: " + \
event.dest_path
if(self.update == False):
file = open("DoNotDelete.txt", "a+")
file.write(self.currentEvent + "\n")
file.close()
else:
print self.currentEvent
def get_boolean(self, bool):
self.update = bool
if __name__ == "__main__":
booleanUpdate = False
updateOrNot = raw_input("Would you like to enable auto-update (y/n): ")
if((len(updateOrNot) == 1) and (updateOrNot.lower() == "y")):
booleanUpdate = True
event_handler = MyHandler(ignore_patterns='*.txt')
event_handler.get_boolean(booleanUpdate)
observer = Observer()
observer.schedule(event_handler, path='.', recursive=True)
observer.start()
print booleanUpdate
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
有关如何解决此错误的任何建议?
答案 0 :(得分:1)
我猜你应该使用模式列表。
class MyHandler(PatternMatchingEventHandler):
currentEvent = ""
update = False
def __init__(self):
super(MyHandler, self).__init__(ignore_patterns=["*/DoNotDelete.txt"])
def doExtractMethods(self):
if (self.update == False):
file_ = open("DoNotDelete.txt", "a+")
file_.write(self.currentEvent + "\n")
file_.close()
else:
print self.currentEvent
def on_modified(self, event):
super(MyHandler, self).on_modified(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", modified, " + event.src_path
self.doExtractMethods()
def on_created(self, event):
super(MyHandler, self).on_created(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", created, " + event.src_path
self.doExtractMethods()
def on_deleted(self, event):
super(MyHandler, self).on_deleted(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", deleted, " + event.src_path
self.doExtractMethods()
def on_moved(self, event):
super(MyHandler, self).on_moved(event)
what = 'Directory' if event.is_directory else 'File'
self.currentEvent = what + ", moved, from: " + event.src_path + ", to: "\
+ event.dest_path
self.doExtractMethods()
def get_boolean(self, bool):
self.update = bool
注意ignore_patterns=["*/DoNotDelete.txt"]
请参阅Monitoring a single file和https://github.com/gorakhargosh/watchdog/issues/99