Python 官方文档:入门教程 => 点击学习
直接上代码: import importlib from watchdog.observers import Observer from watchdog.events import * class ScriptEventHa
直接上代码:
import importlib
from watchdog.observers import Observer
from watchdog.events import *
class ScriptEventHandler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)
# 文件移动
def on_moved(self, event):
if event.is_directory:
print("directory moved from {0} to {1}".fORMat(event.src_path, event.dest_path))
else:
print("file moved from {0} to {1}".format(event.src_path, event.dest_path))
# 文件新建
def on_created(self, event):
if event.is_directory:
print("directory created:{0}".format(event.src_path))
else:
self.reload_module(event.src_path)
print("file created:{0}".format(event.src_path))
# 文件删除
def on_deleted(self, event):
if event.is_directory:
print("directory deleted:{0}".format(event.src_path))
else:
print("file deleted:{0}".format(event.src_path))
# 文件修改
def on_modified(self, event):
if event.is_directory:
print("directory modified:{0}".format(event.src_path))
else:
print("file modified:{0}".format(event.src_path))
self.reload_module(event.src_path)
# 重载模块
def reload_module(self, module_name: str):
module_name = module_name.replace('/', '.')
module_name = module_name.replace('.py', '')
instance = importlib.import_module(module_name)
if instance:
importlib.reload(instance)
observer = Observer()
event_handler = ScriptEventHandler()
# ./test为需要监控的目录
self.observer.schedule(event_handler,'./test', False)
self.observer.start()
--结束END--
本文标题: Python-使用watchdog热更新
本文链接: https://lsjlt.com/news/184407.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0