Python实现的ini文件操作类分享
类代码:
#-*-coding:gbk-*- importConfigParser,os classINIFILE: def__init__(self,filename): self.filename=filename self.initflag=False self.cfg=None self.readhandle=None self.writehandle=None defInit(self): self.cfg=ConfigParser.ConfigParser() try: self.readhandle=open(self.filename,'r') self.cfg.readfp(self.readhandle) self.writehandle=open(self.filename,'w') self.initflag=True except: self.initflag=False returnself.initflag defUnInit(self): ifself.initflag: self.readhandle.close() self.writehandle.closse() defGetValue(self,Section,Key,Default=""): try: value=self.cfg.get(Section,Key) except: value=Default returnvalue defSetValue(self,Section,Key,Value): try: self.cfg.set(Section,Key,Value) except: self.cfg.add_section(Section) self.cfg.set(Section,Key,Value) self.cfg.write(self.writehandle)