首页 > 编程语言 > 详细

【Python】读取ini配置文件

时间:2021-09-15 12:48:02      阅读:27      评论:0      收藏:0      [点我收藏+]

环境配置信息保存在ini文件中,python读取ini配置文件方法

[LoginElement]
username = id > userid
password = id > password

 

class ReadIni:

    def get_value(self, section, key):
        curpath = os.path.abspath(os.path.join(os.getcwd(), "../config"))
        cfgpath = os.path.join(curpath, LocalElement.ini)
        # 创建管理对象
        conf = configparser.ConfigParser()

        # 读ini文件
        conf.read(cfgpath)

        # 获取所有的section
        sections = conf.sections()
        print(sections)

        # 获取指定section下面的key/value值
        # items = conf.items(‘LoginElement‘)
        value = conf[section][key]
        return value


ri = ReadIni()
ReadIni.get_value(ri, LoginElement, username)

 

【Python】读取ini配置文件

原文:https://www.cnblogs.com/Lina-zhu/p/15269277.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!