在这里我需要把一个目录压缩,这就要将目录里的文件一个文件一个文件的加入,然后在使用zipfile的ZipFile类的时候,再一个一个写入压缩文件。
代码
1#!/usr/bin/env python
2#coding=utf-8
3
4import os
5import zipfile
6
7filelist = []
8for root, dirs, files in os.walk("D:\\clean"):
9 for name in files:
10 filelist.append(os.path.join(root, name))
11
12zf = zipfile.ZipFile("d:\\test.zip", "w", zipfile.zlib.DEFLATED)
13for tar in filelist:
14 zf.write(tar)
15zf.close()
2#coding=utf-8
3
4import os
5import zipfile
6
7filelist = []
8for root, dirs, files in os.walk("D:\\clean"):
9 for name in files:
10 filelist.append(os.path.join(root, name))
11
12zf = zipfile.ZipFile("d:\\test.zip", "w", zipfile.zlib.DEFLATED)
13for tar in filelist:
14 zf.write(tar)
15zf.close()