• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中使用gzip模块压缩文件的简单教程

Python中使用gzip模块压缩文件的简单教程

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python gzip模块,python gzip,python gzip解压,python gzip.open,gzip模块等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

压缩数据创建gzip文件
先看一个略麻烦的做法
 

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()
</div>

但其实有个快捷的封装,不用用到StringIO模块
 

f = gzip.open('file.gz', 'wb')
f.write(content)
f.close()
</div>

压缩已经存在的文件
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)
</div>

如果不考虑跨平台,只在linux平台,下面这种方式更直接
 

from subprocess import check_call
check_call('gzip /path/to/file',shell=True)
</div>

</div>

您可能想查找下面的文章:

  • Python实现压缩与解压gzip大文件的方法
  • Python实现压缩与解压gzip大文件的方法
  • Python中使用gzip模块压缩文件的简单教程
  • Python中使用gzip模块压缩文件的简单教程

相关文章

  • Python实现3行代码解简单的一元一次方程
  • 两个使用Python脚本操作文件的小示例分享
  • python实现在控制台输入密码不显示的方法
  • 详解Python程序与服务器连接的WSGI接口
  • Python splitlines使用技巧
  • Python实现检测代理IP是否可以翻墙
  • Python实现将DOC文档转换为PDF的方法
  • Python 专题一 函数的基础知识
  • python的几种开发工具介绍
  • python类继承用法实例分析

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中的二叉树查找算法模块使用指南
    • python基础教程之基本内置数据类型介绍
    • Python实现的检测网站挂马程序
    • 小小聊天室Python代码实现
    • python统计文本文件内单词数量的方法
    • Python max内置函数详细介绍
    • Python实现网络端口转发和重定向的方法
    • 利用python代码写的12306订票代码
    • Django中处理出错页面的方法
    • Python功能键的读取方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有