天天看点

python学习笔记之读取配置文件

1.配置一个.int文件用来写配置文件,例如

2.基本的读操作

-read(filename)               直接读取文件内容

-sections()                      得到所有的section,并以列表的形式返回

-options(section)            得到该section的所有option

-items(section)                得到该section的所有键值对

-get(section,option)        得到section中option的值,返回为string类型

-getint(section,option)    得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

在对配置文件进行读写操作前,我们需要先进行以下两个操作:

实例化ConfigParser对象:  

         2.读取配置文件

然后进行配置文件的读取操作

基本的写入操作:

-write(fp)  将config对象写入至某个 .init 格式的文件  Write an .ini-format representation of the configuration state.

-add_section(section)   添加一个新的section

-set( section, option, value   对section中的option进行设置,需要调用write将内容写入配置文件

-remove_section(section)  删除某个 section

-remove_option(section, option) 

配置文件中的名字是不区分大小写的,如下两个是等价的