python 3.7 一: 操作config 配置文件

persistent.config

[db]
host = 127.0.0.1
port = 3306
user = root
pass = 123456
schema = mytest

[hdfs]
name_node = http://localhost:50070
user = ad

read_config.py

import configparser
import os


class ConfigUtil:

    prop = {}

    def get_prop(self, key_type, key):
        return ConfigUtil.prop[key_type][key]

    def set_prop(self, path, file_name):
        os.chdir(path)
        config = configparser.ConfigParser()
        config.read(file_name, encoding="utf-8")
        db_items = config.items("db")
        hdfs_items = config.items("hdfs")

        db_dict = {}
        hdfs_dict = {}
        for x in db_items:
            key = x[0]
            db_dict[key] = x[1]
        #
        for x in hdfs_items:
            key = x[0]
            hdfs_dict[key] = x[1]

        ConfigUtil.prop['db'] = db_dict
        ConfigUtil.prop['hdfs'] = hdfs_dict

你可能感兴趣的:(python,config)