Python-监听文件变化

Python 监听单个文件变化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class 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()