python 3.4递归压缩一个目录包含子目录所有文件【备份文件】
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-06-12 15:38:10
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
import zipfile import os # 递归目录 def dfs_get_zip_file(input_path, result, ignore=[]): files = os.listdir(input_path) for file in files: filePath = input_path + '/' + file if file in ignore: print("ignored: " + filePath) continue if os.path.isdir(filePath): dfs_get_zip_file(filePath, result, ignore) else: print("added: " + filePath) result.append(filePath) # 输入路径 E:/phpserver # 输出路径 E:/wok.zip # 忽略文件或目录名 def zip_path(input_path, output_path, ignore=[]): outdir = os.path.dirname(output_path) if not os.path.isdir(outdir): os.makedirs(outdir) f = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) filelists = [] dfs_get_zip_file(input_path, filelists, ignore) print('start compress file...') for file in filelists: print('compress: ' + file) file = file.replace('\\', '/') input_path = input_path.replace('\\', '/') f.write(file, file.replace(input_path, '')) f.close() print('compress complete!') return output_path if __name__ == '__main__': zip_path('E:/phpserver', 'E:/wok.zip', ['QuickPHP.ini'])