python如何压缩新文件到已有ZIP文件
2018-03-14 11:32
921 查看
本文为大家分享了python压缩新文件到已有ZIP文件的具体代码,供大家参考,具体内容如下
要点在于使用Python标准库zipfile创建压缩文件时,如果使用'a'模式时,可以追加新内容。
from zipfile import ZipFile from os import listdir from os.path import isfile, isdir, join def addFileIntoZipfile(srcDir, fp): #遍历该文件夹中所有文件 for subpath in listdir(srcDir): subpath = join(srcDir, subpath) if isfile(subpath): #如果是文件就直接压缩到ZIP文件 fp.write(subpath) elif isdir(subpath): #如果是子文件夹就先写入子文件夹名 #然后再递归调用函数 #把所有文件都压缩进入ZIP文件 fp.write(subpath) addFileIntoZipfile(subpath, fp) def zipCompress(srcDir, desZipfile): with ZipFile(desZipfile, mode='a') as fp: addFileIntoZipfile(srcDir, fp) #测试函数功能 paths = [r'C:\python34\Scripts', r'C:\python34\Dlls', r'c:\eclipse'] for path in paths: zipCompress(path, 'test.zip')
来源:python小屋
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Python压缩解压缩zip文件及破解zip文件密码的方法
- python压缩文件夹内所有文件为zip文件的方法
- Python压缩解压缩zip文件及破解zip文件密码的方法
- Python压缩解压缩zip文件及破解zip文件密码的方法
- python压缩文件夹内所有文件为zip文件的方法
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- 如何不解压查看压缩文件的内容
- 如何使用Python3读写INI配置文件
- 如何用PowerPoint压缩ppt文件大小
- 如何给python安装.whl文件
- 如何在Python的.py文件里输入中文字符
- Python脚本如何生成Windows可执行文件.exe
- 高压缩文件是如何实现的
- 如何在Windows下将文件压缩成tgz格式的压缩包 linux系统 centos下
- 利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
- 《OD学hadoop》在LINUX下如何将tar压缩文件解压到指定的目录下
- python暴力破解含密码的zip压缩文件
- Python如何从一个文件中调用另一个文件的类
- 使用Python读写及压缩和解压缩文件的示例