Python-监听文件变化 发表于 2017-09-15 | 分类于 Python | | Python 监听单个文件变化。 1234567891011121314151617181920212223class FileEventHandler(FileSystemEventHandler): def __init__(self): FileSystemEventHandler.__init__(self) def on_created(self, event): if event.is_directory == False and event.src_path == '文件路径': print("system conf change:{0}".format(event.src_path)) def on_modified(self, event): if event.is_directory == False and event.src_path == '文件路径': print("system conf change:{0}".format(event.src_path))if __name__ == "__main__": observer = Observer() event_handler = FileEventHandler() observer.schedule(event_handler, '文件目录', False) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()