python的configparser模块

configparser模块是专门用来读取 config.ini 配置文件,config.ini 配置文件的文件格式算是约定俗成的一种规则:

python的configparser模块

[HTTP] [EMAIL] 在文件中是section。

1:比如要获取section的值:

config=configparser.ConfigParser()   # 生成configparser 对象

config.read(config.ini,  encoding='utf-8')     #读取配置文件

all_sections=config.sections()    #获取所有节点,返回的是一个列表

print('sections', all_sections)

python的configparser模块

python的configparser模块

2:要获取指定节点的所有配置信息

以列表形式返回某个节点的所有配置信息:

items=config.items('HTTP')

python的configparser模块

python的configparser模块

3.获取某个节点section 的所有key值:(只有key值,不包含 value)

options=config.options('HTTP')      #以列表形式返回

print(options)

4.获取执行节点下面的key的value值:

url=config.get('HTTP', 'baseurl')     #返回字符串类型

返回:127.0.0.1

python的configparser模块

python的configparser模块

python的configparser模块

python的configparser模块

python的configparser模块

python的configparser模块

python的configparser模块

python的configparser模块