1. 程式人生 > >shutil模塊(2)——壓縮目錄、文件

shutil模塊(2)——壓縮目錄、文件

~~ 文件 utf-8 lose ext extract xtra 目錄 cnblogs

打包目錄

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()

shutil模塊(2)——壓縮目錄、文件