#获取内容方式1 class configObj: def __init__(self,path = 'my.conf'): #实例化对象 self.conf = configparser.ConfigParser() #读取配置文件 self.cf = self.conf.read(path,encoding='utf8') def getvalue(self,section_name,option_name): #获取内容方式1 value = self.conf.get(section_name,option_name) return value #获取内容方式2 a1 = self.conf.getint('logs','logs_name') print(type(a1),a1) c = configObj() c.getvalue()
#得到所有的sectin,以列表形式返回 sections #得到该section的所有option options(section) #得到该section的所有键值对 items(section) #得到section中option的值,返回为String类型 get(section.option) #得到section中option的值,返回为int类型,还有对应的getbolean()和getfloat()函数 getint()