飘逸的python - 简明gzip模块压缩教程
来源:互联网 发布:php删除数组重复元素 编辑:程序博客网 时间:2024/04/29 23:57
飘逸的python - 简明gzip模块压缩教程
分类: Python2013-07-22 07:55 109人阅读 评论(0) 收藏 举报
gzip
目录(?)[+]
压缩数据创建gzip文件
先看一个略麻烦的做法
但其实有个快捷的封装,不用用到StringIO模块
- import StringIO,gzip
- content = 'Life is short.I use python'
- zbuf = StringIO.StringIO()
- zfile = gzip.GzipFile(mode='wb', compresslevel=9, fileobj=zbuf)
- zfile.write(content)
- zfile.close()
但其实有个快捷的封装,不用用到StringIO模块
- f = gzip.open('file.gz', 'wb')
- f.write(content)
- f.close()
压缩已经存在的文件
python2.7后,可以用with语句
- import gzip
- with open("/path/to/file", 'rb') as plain_file:
- with gzip.open("/path/to/file.gz", 'wb') as zip_file:
- zip_file.writelines(plain_file)
如果不考虑跨平台,只在linux平台,下面这种方式更直接
- from subprocess import check_call
- check_call('gzip /path/to/file',shell=True)
- 飘逸的python - 简明gzip模块压缩教程
- 飘逸的python - 简明gzip模块压缩教程
- 飘逸的python - yield简明教程
- 简明Python 教程 --模块
- python gzip模块实现文件压缩的方法
- 飘逸的python - 简单探索time模块
- 简明Python教程 --- 8.模块
- python简明教程-os模块
- 《简明Python教程》之模块
- Python Gzip压缩与解压模块
- 简明python教程 --C++程序员的视角(三):模块
- 飘逸的python - zlib压缩存到数据库
- Python--模块(Python简明教程)
- 飘逸的python - import上一级目录的模块
- 简明Python教程读书笔记-6 模块
- python简明教程学习笔记3--模块
- 简明Python教程 8)模块 9)数据结构
- 简明Python教程学习笔记_3_模块
- Hadoop后时代
- 《拆掉思维里的墙》简评和部分摘录
- 内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理]
- Ext.net环境配置
- 基于Android平台的鼠标设计与实现
- 飘逸的python - 简明gzip模块压缩教程
- uva 10140 - Prime Distance
- 如何设置代理
- 【Android-Architecture】Android核心模块及相关技术
- javascript中正则表达式中的问号(?) 总结
- struts2的result-type详解 .
- 130720CF div2解题报告
- ffmpeg中获取字节流格式的两种方式
- 首尾相连数组的最大子数组和