'''
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. tyvj P1050 最长公共子序列

    题目链接:http://tyvj.cn/p/1050 题解: 裸题,只是为了测试LCS模板写对没有…… #include<cstdio> #include<cstring> # ...

  2. Python中如何Debug

    debug是编码是非常重要的调试技巧,通过在运行过程中设置断点,帮助开发人员更好的理解运行过程. Python中debug不像JAVA或者C++那样在IDE中设置断点那么直观. Python的debu ...

  3. 《深入理解Java虚拟机》笔记--第三章 、垃圾收集器与内存分配策略

    1960年诞生于MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语言. Java的垃圾收集(Garbage Collection)主要关注堆和方法区的内存回收. 在GC堆进行回收前,第一件 ...

  4. Web服务器处理动态程序三种方式及Apache配置

    模块.CGI.FastCGI三种方式介绍 以PHP脚本为例: 模块方式是指Web服务器通过libphp5.so模块调用PHP服务,模块将相关函数嵌入Web服务请求处理流程,不需要额外解释器进程.注意, ...

  5. 数学中的Sin和Cos是什么意思?(转)

    数学中的Sin和Cos是什么意思? 作者:admin 分类:生活随笔 发表于 2012年03月21日 16:48 问:数学中的Sin和Cos是什么意思? 答:sin, cos, tan 都是三角函数, ...

  6. C++随笔(2)

    在牛客网上刷题,遇到的一些需要注意的题 1.这题需要注意的是strcpy复制的时候什么时候停止 2.这题是关于strlen的,它不统计‘\0',但复制的时候仍会复制. 3.这题是写strcpy函数的, ...

  7. csu 1329 一行盒子(链表操作)

    1329: 一行盒子 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 693  Solved: 134 [Submit][Status][Web Boa ...

  8. Python 读写xlsx

    # pip install openpyxl # openpyxl只能用于处理xlsx,不能用于处理xlsfrom openpyxl import load_workbook # 打开文件ExcelF ...

  9. oracle 12C安装问题

    1. 先弄好c$ share的问题  2. 测试一下 c$ share 是否成功. 方法是在cmd里打net use \\localhost\c$ 失败会是这样子...: 系统错误53  The ne ...

  10. day5时间复杂度

    时间复杂度       (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花 ...