Python针对特定服务定制的代理工具V2.0------(zipdeal.py)
2017-04-22 00:00
811 查看
zipdeal.py:处理gzip文件,进行读取、写入。源代码如下:
在这行这个代码前,需要手动在项目下添加个目录inputJson目录。
#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目录。
相关文章推荐
- Python针对特定服务定制的代理工具V2.0------(zipdeal.py)
- Python针对特定服务定制的代理工具V2.0------(zipdeal.py)
- Python针对特定服务定制的代理工具V2.0------(zipdeal.py)
- Python针对特定服务定制的代理工具V2.0----------(__init__.py与Run.py)
- Python针对特定服务定制的代理工具V2.0------(proxyHandler.py)
- Python针对特定服务定制的代理工具V2.0------(server.py)
- Python针对特定服务定制的代理工具V2.0----------(__init__.py与Run.py)
- Python针对特定服务定制的代理工具V2.0------(server.py)
- Python针对特定服务定制的代理工具V2.0------(server.py)
- Python针对特定服务定制的代理工具V2.0------(proxyHandler.py)
- Python针对特定服务定制的代理工具V2.0------(proxyHandler.py)
- Python针对特定服务定制的代理工具V2.0------(server.py)
- Python针对特定服务定制的代理工具V2.0------(proxyHandler.py)
- Python针对特定服务定制的代理工具V2.0----------(__init__.py与Run.py)
- Python针对特定服务定制的代理工具V2.0----------(__init__.py与Run.py)
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- Python针对特定服务定制的代理工具V2.0----------------(代码组织简介)
- Python针对特定服务定制的代理工具V2.1-------(简述与2.0的区别之处)