You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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 ( )