shutil模块(2)——压缩目录、文件

时间:2022-07-07 19:38:44

打包目录

1 #_*_coding:utf-8_*_
2 #__author__ = "csy"
3 import shutil
4
5 shutil.make_archive("test","zip","D:/5") #生成文件名test,格式zip,目录路径

另外也可以用zipfile模块,指定具体要压缩的文件

1 #_*_coding:utf-8_*_
2 #__author__ = "csy"
3
4 import zipfile
5 z = zipfile.ZipFile("5.zip","w")
6 z.write("1.txt")
7 print("=========~~~~~~~~~~")
8 z.write("3.txt")
9 z.close()

解压

1 #_*_coding:utf-8_*_
2 #__author__ = "csy"
3
4 import zipfile
5 z = zipfile.ZipFile("5.zip","r")
6 z.extractall()
7 z.close()