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

来源:互联网 发布:js qq客服代码 编辑:程序博客网 时间:2024/06/04 01:14

zipdeal.py:处理gzip文件,进行读取、写入。源代码如下:

#coding=utf8import osimport gzipimport timeclass 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 eif __name__ == '__main__':    GzipClass().GZFile("hellO")

在这行这个代码前,需要手动在项目下添加个目录inputJson目录。

0 0
原创粉丝点击