2、读配置文件
import ConfigParser
cf=ConfigParser.ConfigParser()
cf.read(path) 读配置文件(ini、conf)返回结果是列表
cf.sections() 获取读到的全部sections(域),返回列表类型
cf.options('sectionname') 某个域下的全部key,返回列表类型
cf.items('sectionname') 某个域下的全部key,value对
value=cf.get('sectionname','key') 获取某个yu下的key对应的value值
cf.type(value) 获取的value值的类型
(1)getint(section, option)
获取section中option的值,返回int类型数据,因此该函数只能读取int类型的值。
(2)getboolean(section, option)
获取section中option的值,返回布尔类型数据,因此该函数只能读取boolean类型的值。
(3)getfloat(section, option)
获取section中option的值,返回浮点类型数据,因此该函数只能读取浮点类型的值。
(4)has_option(section, option)
检测指定section下是否存在指定的option,若是存在返回True,不然返回False。
(5)has_section(section)
检测配置文件中是否存在指定的section,若是存在返回True,不然返回False。
3、动态写配置文件
cf.add_section('test') 添加一个域
cf.set('test3','key12','value12') 域下添加一个key value对
cf.write(open(path,'w')) 要使用'w'