python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐
'''
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文件_完整版_博主推荐的更多相关文章
- python开发_xml.dom_解析XML文档_完整版_博主推荐
在阅读之前,你需要了解一些xml.dom的一些理论知识,在这里你可以对xml.dom有一定的了解,如果你阅读完之后. 下面是我做的demo 运行效果: 解析的XML文件位置:c:\\test\\hon ...
- python开发_configparser_解析.ini配置文件工具_完整版_博主推荐
# # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...
- python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐
## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...
- python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐
我使用的python版本为:3.3.2 如果你对python中tkinter模块的菜单操作不是很了解,你可以看看: python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推 ...
- python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了.这篇b ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 强连通图(最多加入几条边使得图仍为非强连通图)G - Strongly connected HDU - 4635
题目链接:https://cn.vjudge.net/contest/67418#problem/G 具体思路:首先用tarjan缩点,这个时候就会有很多个缩点,然后再选取一个含有点数最少,并且当前这 ...
- android ViewPager之OnPageChangeListener接口
项目中在使用ViewPager的时候,一般都要在界面滑动的时候做一些事情,android中有个专门的状态回调接口OnPageChangeListener. /** * Callback interfa ...
- 64_q1
QMsgBox-0-9.20130830git94677dc.fc26.i686.rpm 13-Feb-2017 23:40 40674 QMsgBox-0-9.20130830git94677dc. ...
- 148.Sort List---链表排序(冒泡、归并)
题目链接 题目大意:对链表进行排序,要求时间复杂度是o(nlgn). 法一:冒泡,不交换结点,而交换结点中的数值.超时了.代码如下: public ListNode sortList(ListNode ...
- linux系统性能排查命令
[top] 命令可以动态查看当前系统的资源情况,以及占用资源的命令列表 用法: - ctrl + c / q : 停止此命令运行 - c : 展示完整的命令 - [top -bn1]:可以不动态的展示 ...
- Tutorial 7: Schemas & client libraries
转载自:http://www.django-rest-framework.org/tutorial/7-schemas-and-client-libraries/ Tutorial 7: Schema ...
- Window文本在Linux中出现的^M问题
问题:在Windows中写了一个shell脚本在Linux中死活不能运行,怎么也查不出错误,原来是格式问题. 原因:Windows/DOS系统的换行符是/r/n,Unix/Linux系统的换行符是/n ...
- 【转载】移动开发中的上下左右滑动插件jquery.swipe.js
原文地址http://blog.csdn.net/pvfhv/article/details/3449803/# 源码: (function($) { var old = $.fn.swipe; $. ...
- linux 命令route add default dev eth0和route add default gw eth0的区别?
https://blog.csdn.net/zhaogezhuoyuezhao/article/details/7339220
- 易普优APS高级计划排程系统系列提纲:行业知识,业务建模,排程算法,计划可视化,平台框架,案例分享
专注于高级计划排程系统研发与实施10来年了,国内外各种APS软件基本都研究过,这里列个提纲主要从6个方面跟大家一起讨论分享,欢迎大家鼓掌或拍砖 易普优APS高级计划排程系统系列001:行业知识,APS ...