Python-对文件夹zip压缩处理

Python的文件压缩

做flask开发工具用到zip文件压缩,亲测通过,贴上来。

import zipfile
import os

def zip_yasuo(start_dir):
    file_news = start_dir + '.zip'                                    
    z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)        
    for dir_path, dir_names, file_names in os.walk(start_dir):
        file_path = dir_path.replace(start_dir, '')     
        file_path = file_path and file_path + os.sep or ''         
        for filename in file_names:
            z.write(os.path.join(dir_path, filename), file_path+filename)
    z.close()

if __name__ == "__main__":
    Start_dir = "static/uploads/my_zip"
    zip_yasuo(Start_dir)

你可能感兴趣的:(python)