GPT-4还是要聪明一点,生成的脚本质量更好一些。
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import os
from datetime import datetime
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.is_directory:
return None
if event.event_type == 'modified':
# 获取文件修改时间
timestamp = os.path.getmtime(event.src_path)
modify_time = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
# 获取相对路径
relative_path = os.path.relpath(event.src_path, '/var/img')
# 生成HTML文件
with open('/var/www/img.html', 'w') as f:
f.write(f'<img src="{relative_path}" alt="Image"><p>{modify_time}</p>')
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='/var/img', recursive=False)
observer.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
break
observer.join()
--
修改:poocp FROM 171.221.52.*
FROM 171.221.52.*