python解压缩gz文件_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环境变量(转)

默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1.首先需要在系统中注册python环境变量:假设python的安装路 ...

eclipse的使用一

The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files ...

Scala:条件表达式的好处

条件表达式的好处之一是:让代码更简洁,例如在一个需要根据不同条件收集不同值的场景中,多数语言提供的代码如下: ; ) { tmp = xxx; } ) { tmp = yyy; } else { tm ...

mysql修改用户密码 新增用户

修改密码: mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by 'my_password_new ...

asp.net 中使用JQuery Ajax 上传文件

首先创建一个网页,网页中添加如下代码.

Upload File using Jquery AJAX in Asp.net

实现当UILable的内容超出其范围后自动滚动效果

本文主要介绍 [当UILabel的内容超出其自身的宽度范围后,进行互动展示的效果],我们先来看一下Demo的效果图. 实际实现起来并不十分繁杂,在这里,为了开发的效率,我们使用了一个已经封装好的UIL ...

模板-->Matrix重载运算符:+,-,x

如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 poj_2118_Firepersons,my_ac_code 简单的测试 INPUT: 1 2 3 1 3 4 3 -1 ...

使用秘钥连接ssh

ssh服务器搭建 通过秘钥登陆连接另外一台虚拟机 创建证书  ssh-keygen -t rsa 第一行密钥保存位置直接输入回车 确定默认创建位置为 /root/.ssh 公钥必须改名为  autho ...

openfire源码解读--用户登录

根据xmpp协议 客户端发送: XXXXXXXXXXXXX ...

js中 substr(), substring(), slice()的区别

一.作用 三者都是基于原字符串创建新字符串的方法. 接收一到两个参数,第一个参数截取字符串的开始位置(字符下标,从0开始),第二个参数因方法不同而不同,后面不同点会说到. 另外,三个方法都不会修改原字 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值