-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCATS.py
More file actions
64 lines (52 loc) · 2.03 KB
/
CATS.py
File metadata and controls
64 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 愿你早日摆脱CATS的苦海
import zipfile
import os
import datetime
import py7zr
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
def GetDesktopPath():
return os.path.join(os.path.expanduser("~"), 'Desktop')
def Makezip(file_dir):
start_dir = 'C:\\XOSCATS'
my_zip = zipfile.ZipFile(file_dir, "w", zipfile.ZIP_DEFLATED)
for dir_path, dirs, filenames in os.walk(start_dir):
for filename in filenames:
my_zip.write(os.path.join(dir_path, filename))
my_zip.close()
print(f'备份文件为:{file_dir}')
print("备份成功,祝你学习进步")
def Make7z(file_dir):
start_dir = 'C:\\XOSCATS'
archive = py7zr.SevenZipFile(file_dir, 'w')
for dir_path, dir_names, file_names in os.walk(start_dir):
for filename in file_names:
fpath = dir_path.replace(start_dir, '')
file_path = os.path.join(dir_path, filename)
filename = os.path.join(fpath, filename)
archive.write(file_path, arcname=filename)
archive.close()
print(f'备份文件为:{file_dir}')
print("备份成功,祝你学习进步")
def main():
mkdir('D:\\CATS备份')
print("默认zip格式备份,速度快,但压缩效率低,压缩文件大。\n7z备份压缩时间长,但压缩文件小。")
a = input('(0)默认zip压缩包备份\n(1)7z模式压缩备份\n(2)备份到桌面\n')
now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
print(f'当前时间为:{now_time}')
if a == '0':
file_dir = f'D:\\CATS备份\\CATS{now_time}.zip'
Makezip(file_dir)
elif a == '1':
file_dir = f'D:\\CATS备份\\CATS{now_time}.7z'
Make7z(file_dir)
elif a == '2':
file_dir = f"{GetDesktopPath()}\\CATS{now_time}.zip"
Makezip(file_dir)
else:
Makezip(f'D:\\CATS备份\\CATS{now_time}.zip')
input("按Enter键以退出脚本\n")
if __name__ == '__main__':
main()