watchdog (Python's library) - How to send signal when a file is modified?
我的用于检测何时修改了特定文件的类的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class MyEventHandler(FileSystemEventHandler, QtCore.QThread): def __init__(self, filename): super(MyEventHandler, self).__init__() self.filename = filename def on_modified(self, event): if not event.is_directory and event.src_path.endswith(self.filename): print"modified" self.emit(QtCore.SIGNAL("fileModified")) class WatchOutForFileModifications(QtCore.QThread): def __init__(self, path, filename): super(WatchOutForFileModifications, self).__init__() self.path = path self.filename = filename self.observer = Observer() self.event_handler = MyEventHandler(self.filename) self.observer.schedule(self.event_handler, self.path, recursive=False) self.observer.start() def run(self): while 1: self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified) def modified(self): self.emit(QtCore.SIGNAL("fileModified1")) |
以及应用程序本身的代码段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() uic.loadUi('test.ui', self) path ="somePath" filename ="someName" self.fileWatcher = WatchOutForFileModifications(path, filename) self.fileWatcher.start() self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified) self.show() def fileModified(self): print 1 |
问题是,当文件被修改时,我得到一个不停的1正在打印的流。 我意识到在
编辑
一些修改后的工作代码:
导入系统
来自PyQt4导入QtGui,QtCore,uic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | from watchdog.events import FileSystemEventHandler from watchdog.observers import Observer class MyEventHandler(FileSystemEventHandler, QtCore.QThread): def __init__(self, filename): super(MyEventHandler, self).__init__() self.filename = filename self.signalName = str(filename) +"_modified" def on_modified(self, event): if not event.is_directory and event.src_path.endswith(self.filename): self.emit(QtCore.SIGNAL(self.signalName)) class FileModificationWatcher(QtCore.QThread): def __init__(self, path, filename): super(FileModificationWatcher, self).__init__() self.path = path self.filename = filename self.observer = Observer() self.event_handler = MyEventHandler(self.filename) self.observer.schedule(self.event_handler, self.path, recursive=False) self.observer.start() def run(self): pass def getEmitter(self): return self.event_handler def getSignalName(self): return self.event_handler.signalName class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() uic.loadUi('test.ui', self) path ="somePath" filename ="someName" self.fileWatcher = FileModificationWatcher(path, filename) self.fileWatcher.start() self.connect(self.fileWatcher.getEmitter(), QtCore.SIGNAL(self.fileWatcher.getSignalName()), self.fileModified) self.show() def fileModified(self): print 1 if __name__ =="__main__": app = QtGui.QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_()) |
问题是在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | from PyQt4 import QtCore, QtGui from watchdog.events import FileSystemEventHandler from watchdog.observers import Observer class MyEventHandler(FileSystemEventHandler, QtCore.QThread): def __init__(self, filename): super(MyEventHandler, self).__init__() self.filename = filename def on_modified(self, event): if not event.is_directory and event.src_path.endswith(self.filename): print("modified") self.emit(QtCore.SIGNAL("fileModified")) class WatchOutForFileModifications(QtCore.QThread): def __init__(self, path, filename): super(WatchOutForFileModifications, self).__init__() self.path = path self.filename = filename self.observer = Observer() self.event_handler = MyEventHandler(self.filename) self.observer.schedule(self.event_handler, self.path, recursive=False) self.observer.start() self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified) def run(self): pass def modified(self): self.emit(QtCore.SIGNAL("fileModified1")) class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() path = r'D:\Code\' filename ="Hexagon_Grid_Creation.py" self.fileWatcher = WatchOutForFileModifications(path, filename) #self.fileWatcher.start() self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified) self.show() def fileModified(self): print(1) if __name__ == '__main__': app = QtGui.QApplication([]) window = MainWindow() window.show() app.exec_() |
在这种情况下,您可能不需要
在WatchOutForFileModifications类中,尝试在init方法中连接信号。