多线程
# -*- coding: UTF-8 -*-
import os
import re
import time
from datetime import datetime
import zipfile
import threading
res = os.listdir("D:")
pattern = re.compile("^[M, m]irserver.*")
gb_list = {}
qd_list = {}
fdb_list = {}
id_list = {}
threads = []
backuptime = time.strftime("%Y-%m-%d_%H_%M_%S")
backupdir = "E:/游戏数据备份/全局变量和通区文本/"
if not os.path.isdir(backupdir):
os.makedirs(backupdir)
global_backupfile = backupdir + "gv_qd_" + backuptime + ".zip"
fdb_backupfile = backupdir + "fdb_" + backuptime + ".zip"
id_backupfile = backupdir + "id_" + backuptime + ".zip"
txt_backupfile = backupdir + "txt_" + backuptime + ".zip"
global_z = zipfile.ZipFile(global_backupfile, 'w', zipfile.ZIP_DEFLATED)
fdb_z = zipfile.ZipFile(fdb_backupfile, 'w', zipfile.ZIP_DEFLATED)
id_z = zipfile.ZipFile(id_backupfile, 'w', zipfile.ZIP_DEFLATED)
txt_z = zipfile.ZipFile(txt_backupfile, 'w', zipfile.ZIP_DEFLATED)
# 需要打包文件夹写入到压缩包
def zip_dir(zipobj, mydir):
# print("当前时间是:", datetime.now)
for dirpath, dirnames, filenames in os.walk(mydir):
for filename in filenames:
zipobj.write(os.path.join(dirpath, filename))
for mydir in res:
mir_str = pattern.search(mydir)
if mir_str and os.path.isdir("d:/" + mir_str.group()):
gb_list[mydir] = "d:/" + mir_str.group()+ "/mir200/"+"GlobalVal.ini"
qd_list[mydir] = "d:/" + mir_str.group()+ "/mir200/envir/questdiary/"
fdb_list[mydir] = "d:/" + mir_str.group()+ "/DBServer/FDB/"
id_list[mydir] = "d:/" + mir_str.group()+ "/LoginSrv/IDDB/"
for mir_dir, global_file in gb_list.items():
if os.path.isfile(global_file):
global_z.write(global_file)
for myqd, qt_dir in qd_list.items():
# zip_dir(z, qt_dir)
t = threading.Thread(target=zip_dir,args=(global_z, qt_dir))
threads.append(t)
for myqd, qt_dir in fdb_list.items():
t = threading.Thread(target=zip_dir,args=(fdb_z, qt_dir))
threads.append(t)
for myqd, qt_dir in id_list.items():
# zip_dir(z, qt_dir)
t = threading.Thread(target=zip_dir,args=(id_z, qt_dir))
threads.append(t)
txtdir = "D:/数据文件"
t = threading.Thread(target=zip_dir,args=(txt_z, txtdir))
threads.append(t)
for t in threads:
t.setDaemon(True)
t.start()
t.join()
# for t in threads:
# threading.Thread.join()
global_z.close()
fdb_z.close()
id_z.close()
txt_z.close()