Python gzip库函数使用方法及实例
2016-07-26 15:02
429 查看
<pre name="code" class="python">解压gzip文件示例: import gzip f = gzip.open('file.txt.gz', 'rb') file_content = f.read() f.close() 创建gzip文件: import gzip content = "Lots of content here" f = gzip.open('file.txt.gz', 'wb') f.write(content) f.close() gzip压缩现有文件: import gzip f_in = open('file.txt', 'rb') f_out = gzip.open('file.txt.gz', 'wb') f_out.writelines(f_in) f_out.close() f_in.close()
实例:
从/opt/mat/*.gz文件中读取每一行,找到包含'GET~#~ ~#~'或者'POST~#~ ~#~'的输出该行
#!/usr/bin/python
#!encoding:utf8
import shutil
import os
import sys
import glob
import re
import gzip
#扫描目录文件
def parseFile(filename):
#打开文件,遍历其中的每一行遇到有'GET~#~ ~#~'或者'POST~#~ ~#~'的输出该行
file = gzip.open(filename, 'rb')
#eachline = file.readlines()
for eachline in file:
#str = eachline
#if eachline == "*GET~#~ ~#~*" or eachline == "*POST~#~ ~#~*":
if eachline.find("GET~#~ ~#~") or eachline.find("*POST~#~ ~#~*"):
print(eachline)
file.close()
if __name__ == '__main__':
scandir = '/opt/mat/'
fileform = '*.gz'
flist = glob.glob(os.path.join(scandir, fileform))
for item in flist:
print(item)
parseFile(item)
相关文章推荐
- python第三节课堂练习:统计一句单词
- 如何在交互式python下重复最近输入
- python bz2模块
- Python入门笔记(2)_ 数据类型及取整运算 round,floor,ceil
- 编写高质量代码 改善Python程序的91个建议
- python 序列化之pickle
- python_numpy,简要操作
- python3.4爬网页图片
- nohup & rabbitmq & python
- Python入门笔记(1)_ 安装、IDLE设置 及 opencv 配置
- python在vs2015中的使用
- Python爬虫利器二之Beautiful Soup的用法
- python――多进程、线程、携程
- python collections模块
- python中烦人的锟斤拷(\xef\xbf\xbd)
- python中烦人的锟斤拷(\xef\xbf\xbd)
- python 3 安装 scrapy 并运行成功
- python学习——编写ORM
- 371. Sum of Two Integers [easy] (Python)
- Python爬虫学习心得