Python压缩及解压文件
Zip压缩
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
import zipfile  #加载模块
# 压缩
z = zipfile.ZipFile('test.zip', 'w')  #建立一个名为test的zip压缩文件
z.write('1.txt')   #对同级目录下的1.txt文件进行压缩到test.zip
z.write('2.txt')   #对同级目录下的2.txt文件进行压缩到test.zip
z.close()
Zip解压
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
import zipfile  #加载模块
# 解压
z = zipfile.ZipFile('test.zip', 'r')  #对test.zip进行读取
z.extractall()   #解压所有的文件
z.extract('1.txt')   #只解压1.txt
#解压可设置解压到哪个文件夹
z.close()
Tar压缩
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
import tarfile  #加载模块
# 压缩
tar = tarfile.open('test.tar','w')   #创建一个test.tar文件
tar.add('/1.txt', arcname='11.txt')   #arcname为保存之后的名称
tar.add('/2.txt', arcname='22.txt')
tar.close()
Tar解压
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
import tarfile   #加载模块
# 解压
tar = tarfile.open('test.tar','r')  #对test.tar进行读取
tar.extractall()  #解压全部文件
tar.extract('1.txt')  #解压指定文件
#可设置解压地址
tar.close()
                    
                
                
            
        
浙公网安备 33010602011771号