python实现的ini文件操作类分享

类代码:

# -*- coding:gbk -*-
import configparser, os
class inifile:
def __init__(self, filename):
self.filename = filename
self.initflag = false
self.cfg = none
self.readhandle = none
self.writehandle = none
def init(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
return self.initflag
def uninit(self):
if self.initflag:
self.readhandle.close()
self.writehandle.closse()
def getvalue(self, section, key, default = “”):
try:
value = self.cfg.get(section, key)
except:
value = default
return value
def setvalue(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)

Posted in 未分类

发表评论