python读取ini配置文件

# -*-encoding=utf-8-*-
# 测试ConfigParser
import os
from configparser import ConfigParser

# 初始化
config = ConfigParser()

# 配置文件的绝对路径
config_path = os.path.dirname(os.path.realpath(__file__)) + "/config.ini"

# 读取配置文件
config.read(filenames=config_path,encoding = 'UTF-8')

"""
读取配置信息
"""
# 查看配置中的所有section节点【返回值以列表方式存放了所有section节点名】
sections = config.sections()
print(sections,"所有的sections")

# 返回指定section节点中的的所有option名称【返回值是列表的方式】
section_options = config.options(section="ip") # 返回section中option的值
value = config.get(section="ip", option="ip")
print(value,'value')
# 是否有这个section
print(config.has_section(section="ip1"))

print(config.items(section="ip"))
posted @   木木霖  阅读(263)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示