直接上代码演示
from configparser import ConfigParser
fpath = r'E:\net.ini'
config = ConfigParser()
config.read(fpath)
#获取所有的section名称
print('list section:', list(config.keys()))
#判断section是否存在
if config.has_section('network') and config.has_option('network', 'ip'):
#获取指定section下k的值
print(config.get('network', 'ip'))
else:
print("network or ip error")结果:
list section: ['DEFAULT', 'network', 'user'] 172.22.201.71