通过zipfile模块实现对文件、文件夹的zip压缩和解压
#打包成zip文件 import zipfile f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) f.write('file_to_add.py') f.close() #从zip文件解包 import zipfile zfile = zipfile.ZipFile('archive.zip','r') for filename in zfile.namelist(): data = zfile.read(filename) file = open(filename, 'w+b') file.write(data) file.close() #把整个文件夹内的文件打包 import zipfile f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) startdir = "c://mydirectory" for dirpath, dirnames, filenames in os.walk(startdir): for filename in filenames: f.write(os.path.join(dirpath,filename)) f.close()
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/8340.html