Files
dsProject/dsLightRag/Tools/T1_BackupTopic.py
2025-08-14 15:45:08 +08:00

32 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import zipfile
from datetime import datetime
def backup_topic():
# 源目录路径
source_dir = r'd:\dsWork\dsProject\dsLightRag\Topic'
# 备份目录路径
backup_dir = r'D:\dsWork\dsProject\dsLightRag\Backup'
# 创建备份目录(如果不存在)
os.makedirs(backup_dir, exist_ok=True)
# 生成带时间戳的文件名
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
zip_filename = f'{timestamp}.zip'
zip_path = os.path.join(backup_dir, zip_filename)
# 以最高压缩率创建ZIP文件
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
# 遍历源目录中的所有文件和子目录
for root, dirs, files in os.walk(source_dir):
for file in files:
file_path = os.path.join(root, file)
# 计算相对路径避免在ZIP中包含绝对路径
relative_path = os.path.relpath(file_path, source_dir)
zipf.write(file_path, arcname=relative_path)
print(f"备份完成: {zip_path}")
if __name__ == "__main__":
backup_topic()