gpt4 book ai didi

python - 使用 WatchDog 忽略文件

转载 作者:行者123 更新时间:2023-12-01 05:19:03 26 4
gpt4 key购买 nike

我正在运行以下代码,该代码在 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()

有关如何解决此错误的任何建议吗?

最佳答案

我想你应该使用模式列表。

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

关于python - 使用 WatchDog 忽略文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22736481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com