有时需要用到压缩文件,网上搜集了一段代码;
分享一下:
1 import os 2 import zipfile 3 4 def make_zip(localPath, pname): 5 6 zipf = zipfile.ZipFile(pname, 'w',zipfile.ZIP_DEFLATED) 7 pre_len = len(os.path.dirname(localPath)) 8 for parent, dirnames, filenames in os.walk(localPath): 9 for filename in filenames: 10 pathfile = os.path.join(parent, filename) 11 arcname = pathfile[pre_len:].strip(os.path.sep) 12 zipf.write(pathfile, arcname) 13 zipf.close() 14 15 if __name__ == "__main__": 16 path = "D:\novel_data\" 17 print "start zip" 18 make_zip(path,"D:\novel_data.zip")