您的位置:首页 > 编程语言 > Python开发

Python针对特定服务定制的代理工具V2.0------(zipdeal.py)

2017-04-22 00:00 811 查看
zipdeal.py:处理gzip文件,进行读取、写入。源代码如下:

#coding=utf8
import os
import gzip
import time
class GzipClass():
def __init__(self):
try:
'''记录当前运行时间'''
now = time.strftime("%Y%m%d%H%M%S")

'''创建新的文件'''
filName=file(".\\inputJson"+"\\"+now+".gz","wb+")

'''关闭文件'''
filName.close()

'''
创建一个全局变量FILE_PATH
并给变量赋值:gz文件所在路径
'''
self.FILE_PATH =".\\inputJson"+"\\"+now+".gz"
except Exception,e:
print e

#读取gz文件中的内容
def read_file(self,path):
try:
#判断路径是否存在,如果存在打开gz文件并读取
#不存在给出相应的提示信息
if os.path.exists(path):
with gzip.open(path, 'rb') as pf:
return pf.read()
else:
print 'the path %r is not exist!' %(path)
finally:
pf.close()

#把内容写入gz文件
def write_file(self,path, content):
try:
#判断路径是否存在,如果存在打开gz文件并读取
#不存在给出相应的提示信息
if os.path.exists(path):
with gzip.open(path, 'wb') as f:
f.write(content)
else:
print 'the path %r is not exist!' %(path)
finally:
f.close()

def GZFile(self,content):
try:
self.write_file(self.FILE_PATH, content)
con =self.read_file(self.FILE_PATH)
print '#' * 50
print con
except Exception,e:
print e

if __name__ == '__main__':
GzipClass().GZFile("hellO")


在这行这个代码前,需要手动在项目下添加个目录inputJson目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐