'''
gzip -- 支持gzip文件 源文件:Lib/gzip.py 这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip。 数据的压缩源于zlib模块的支持。 在gzip模块提供了GzipFile类,在该类中提供了像open(),compress()和depress()等一些方便的方法
GzipFile类在读写gzip格式的文件的时候,自动的压缩和解压缩数据类似于操作普通的文件对象。 在gzip模块定义了一些方法: gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
打开一个gzip已经压缩好的gzip格式的文件,并返回一个文件对象:file object.
参数filename可以是真是的文件名(a str or bytes对象),或着是已经存在的读写文件对象。
参数mode在操作二进制的时候使用:'r','rb','a','ab','wb'
操作text的时候使用:'rt,'at','wt'
默认是:'rb'
参数compresslevel是0-9的数值。 class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) '''

运行效果:

====================================================

代码部分:

====================================================

 #python gzip module

 #Author : Hongten
#MailTo : hongtenzone@foxmail.com
#QQ : 648719819
#Blog : http://www.cnblogs.com/hongten
#Create : 2013-08-19
#Version: 1.0 import os
import gzip
'''
gzip -- 支持gzip文件 源文件:Lib/gzip.py 这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip。 数据的压缩源于zlib模块的支持。 在gzip模块提供了GzipFile类,在该类中提供了像open(),compress()和depress()等一些方便的方法
GzipFile类在读写gzip格式的文件的时候,自动的压缩和解压缩数据类似于操作普通的文件对象。 在gzip模块定义了一些方法: gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
打开一个gzip已经压缩好的gzip格式的文件,并返回一个文件对象:file object.
参数filename可以是真是的文件名(a str or bytes对象),或着是已经存在的读写文件对象。
参数mode在操作二进制的时候使用:'r','rb','a','ab','wb'
操作text的时候使用:'rt,'at','wt'
默认是:'rb'
参数compresslevel是0-9的数值。 class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) '''
#运行此文件的时候,你只需要创建txt文件的存放位置即可
#gz文件系统可以自动创建 #global var
#是否显示日志信息
SHOW_LOG = True
#gz文件存放位置
GZ_FILE_PATH = ''
#txt文件存放位置
TXT_FILE_PATH = '' def read_gz_file(path):
'''read the existing gzip-format file,and return the content of this file'''
if os.path.exists(path):
#the function open(filename, mode = 'rb'),so the mode argument is default is 'rb'
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
with gzip.open(path, 'rb') as pf:
return pf.read()
else:
print('the path [{}] is not exist!'.format(path)) def write_gz_file(path, content):
'''write the byte-format content into the gzip-format file
so,with this way,we can creat the file if the path doesn't exist.will
we can write the content into the file if the file existing'''
if SHOW_LOG:
print('写入文件:[{}] 内容:[{}]'.format(path, content))
with gzip.open(path, 'wb') as f:
f.write(content) def read_txt_write_gz(tpath, gzpath):
'''read the txt-format file with 'rb' and write this file content
to the gzip-format file'''
if os.path.exists(tpath):
if os.path.exists(gzpath):
if SHOW_LOG:
print('打开文件:[{}]'.format(tpath))
with open(tpath, 'rb') as t:
if SHOW_LOG:
print('打开文件:[{}]'.format(gzpath))
with gzip.open(gzpath, 'wb') as g:
if SHOW_LOG:
print('写入内容:[{}]'.format(t))
g.writelines(t)
if SHOW_LOG:
print('写入内容完成...')
else:
print('the path [{}] is not exist!'.format(gzpath))
else:
print('the path [{}] is not exist!'.format(tpath)) def init():
global SHOW_LOG
SHOW_LOG = True
#gz文件存放位置
global GZ_FILE_PATH
GZ_FILE_PATH = 'c:\\test\\hongten.txt.gz'
#txt文件存放位置
global TXT_FILE_PATH
TXT_FILE_PATH = 'c:\\test\\honngten_info.txt' def main():
init()
content = b'this is a byte message!'
write_gz_file(GZ_FILE_PATH, content)
con = read_gz_file(GZ_FILE_PATH)
print(con)
print('#' * 50)
content_str = 'this is a str message!'
write_gz_file(GZ_FILE_PATH, bytes(content_str, 'utf-8'))
con = read_gz_file(GZ_FILE_PATH)
print(con)
print('#' * 50)
read_txt_write_gz(TXT_FILE_PATH, GZ_FILE_PATH)
con = read_gz_file(GZ_FILE_PATH)
print(con) if __name__ == '__main__':
main()

python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐的更多相关文章

  1. python开发_xml.dom_解析XML文档_完整版_博主推荐

    在阅读之前,你需要了解一些xml.dom的一些理论知识,在这里你可以对xml.dom有一定的了解,如果你阅读完之后. 下面是我做的demo 运行效果: 解析的XML文件位置:c:\\test\\hon ...

  2. python开发_configparser_解析.ini配置文件工具_完整版_博主推荐

    # # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...

  3. python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐

    ## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...

  4. python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

    我使用的python版本为:3.3.2 如果你对python中tkinter模块的菜单操作不是很了解,你可以看看: python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推 ...

  5. python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)

    在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了.这篇b ...

  6. CDH版本大数据集群下搭建Hue(hadoop-2.6.0-cdh5.5.4.gz + hue-3.9.0-cdh5.5.4.tar.gz)(博主推荐)

    不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...

  7. Spark on YARN模式的安装(spark-1.6.1-bin-hadoop2.6.tgz + hadoop-2.6.0.tar.gz)(master、slave1和slave2)(博主推荐)

    说白了 Spark on YARN模式的安装,它是非常的简单,只需要下载编译好Spark安装包,在一台带有Hadoop YARN客户端的的机器上运行即可.  Spark on YARN简介与运行wor ...

  8. hadoop-2.7.3.tar.gz + spark-2.0.2-bin-hadoop2.7.tgz + zeppelin-0.6.2-incubating-bin-all.tgz(master、slave1和slave2)(博主推荐)(图文详解)

    不多说,直接上干货! 我这里,采取的是ubuntu 16.04系统,当然大家也可以在CentOS6.5里,这些都是小事 CentOS 6.5的安装详解 hadoop-2.6.0.tar.gz + sp ...

  9. hadoop-2.6.0.tar.gz + spark-1.6.1-bin-hadoop2.6.tgz + zeppelin-0.5.6-incubating-bin-all.tgz(master、slave1和slave2)(博主推荐)(图文详解)

    不多说,直接上干货! 我这里,采取的是CentOS6.5,当然大家也可以在ubuntu 16.04系统里,这些都是小事 CentOS 6.5的安装详解 hadoop-2.6.0.tar.gz + sp ...

随机推荐

  1. Tinyos Makerules解读

    Makerules 文件解读 位于/opt/tinyos-2.1.2/support/make #-*-Makefile-*- vim:syntax=make #$Id: Makerules,v 1. ...

  2. 64_q1

    QMsgBox-0-9.20130830git94677dc.fc26.i686.rpm 13-Feb-2017 23:40 40674 QMsgBox-0-9.20130830git94677dc. ...

  3. java线上应用故障排查之二:高内存占用【转】

    前一篇介绍了线上应用故障排查之一:高CPU占用,这篇主要分析高内存占用故障的排查. 搞Java开发的,经常会碰到下面两种异常: 1.java.lang.OutOfMemoryError: PermGe ...

  4. JS时间转换的一个坑位

    在做项目的时候,无意发现了一个小东西. new Date('2018-05-15') new Date('2018-5-15') 输出的结果是不同的,相差了8小时.然后让我回忆到之前看的一个时间转换函 ...

  5. 码源中国.gitignore忽略文件配置

    码源中国.gitignore忽略文件配置 ## Ignore Visual Studio temporary files, build results, and ## files generated ...

  6. 设计模式--工厂模式 caffe_layer注册

    来源:http://www.cnblogs.com/zhouqiang/archive/2012/07/20/2601365.html 来源:http://blog.luoyetx.com/2016/ ...

  7. 以太坊go-ethereum客户端(三)两种全节点启动模式

    这篇博客介绍一下go-ethereum全节点的两种启动模式:主网络快速启动和测试网络快速启动.这也是客户端所提供的两种启动方式,直接拿来使用即可.下面具体介绍一下使用方法. 主网络快速启动 其实,我们 ...

  8. hdu 5875(单调栈)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  9. 洛谷P1615 西游记公司 题解

    题目传送门 这道题题面写得非常好. 但好像并没有什么ruan用. 这道题貌似就是把时间差求出来,乘上猪八戒能偷的电脑数就好了.(注意long long) #include<bits/stdc++ ...

  10. 洛谷P1876开灯 题解

    题目传送门 这道题目是道数学题(下面也写了),所以仔细研究发现:N轮之后,只有是小于N的完全平方数的灯能亮着.所以接下来就好办了: #include<bits/stdc++.h> usin ...