2014-03-29 149 views
1

我正在運行以下代碼,該代碼在Python中實現WatchDog以監視文件更改。我試圖跟蹤日誌文件(DoNotDelete.txt)中的所有更改,因此我試圖忽略代碼中的這個DoNotDelete.txt文件。然而,當我嘗試運行這個(首先通過傳遞'n'到文件,然後通過更改文件),我得到一個ValueError(ValueError:衝突模式set(['*'])包括和排除)。使用WatchDog忽略文件

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 filehttps://github.com/gorakhargosh/watchdog/issues/99

+0

@ user3345070這是否解決了您的問題? –