'''
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. BurpSuite 设置Hostname Resolution

    #写在前面 这种情况你可能遇到过: 对方用了CDN, 你查到了对方真实IP, 但还不能100%肯定. 这时候, 最好的测试就是 win/linux修改HOST文件 Win重启电脑 Linux重启网络 ...

  2. linux和windows下TIME_WAIT过多的解决办法

    http://www.51testing.com/html/48/202848-249774.html linux和windows下TIME_WAIT过多的解决办法 http://m.sohu.com ...

  3. linux---centos7 安装chromedriver

    1.安装浏览器 指定yum 源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.re ...

  4. Django 注册

    一. 本地图片上传预览 1. 上传文件框隐藏到图片上面,点击图片相当于点上传文件框 <div class="login"> <div style="po ...

  5. office 文档转pdf

    本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18- ...

  6. mouseover与mouseenter,mouseout与mouseleave的区别

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件.只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseout ...

  7. git客户端基本操作

    首先下载git 一路next安装好了之后,打开任意盘符,右键打开git bash here 首先:初始首次的用户名和邮箱,之后就不用了. git config --global user.name & ...

  8. loadrunner参数取值方法总结

    在参数设置位置有两个地方:Select next row –下一行的取值方式(针对用户)Sequential 顺序的,即所有用户都是按照同一种方式取值(都是按照Update value on方式取值, ...

  9. Java Integer Cache

    Java Integer Cache Java 代码 public class IntegerDemo { public static void main(String[] args) { Integ ...

  10. poj1606 Jugs(BFS)

    题目链接 http://poj.org/problem?id=1606 题意 有两个容量分别为ca,cb的杯子,可以向杯子里倒水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其中 ...