Python之configparser模块的封装

# -*- encoding:utf-8 -*-
from configparser import ConfigParser
import os

class ConfigFileUtils:

    def __init__(self, config_file, encode="utf-8"):
        if os.path.exists(config_file):
            self.__cfg_file = config_file
        else:
            #此处做其他异常处理或创建配置文件操作
            print("配置文件不存在!")
            pass
        self.__config = ConfigParser()
        self.__config.read(config_file, encoding=encode)

	# 获取配置文件的所有section
    def get_sections(self):
        return self.__config.sections()
	
	# 获取指定section的所有option
    def get_options(self,section_name):
        if self.__config.has_section(section_name):
            return self.__config.options(section_name)
        else:
            raise ValueError(section_name)
	
	# 获取指定section下option的value值
    def get_option_value(self, section_name, option_name):
        if self.__config.has_option(section_name, option_name):
            return self.__config.get(section_name, option_name)

	# 获取指定section下的option的键值对
    def get_all_items(self, section):
        if self.__config.has_section(section):
            return self.__config.items(section)

	# 打印配置文件所有的值
    def print_all_items(self):
        for section in self.get_sections():
            print("[" + section + "]")
            for K, V in self.__config.items(section):
                print(K + "=" + V)

	# 增加section
    def add_new_section(self, new_section):
        if not self.__config.has_section(new_section):
            self.__config.add_section(new_section)
            self.__update_cfg_file()

	# 增加指定section下option
    def add_option(self, section_name, option_key, option_value):
        if self.__config.has_section(section_name):
            self.__config.set(section_name, option_key, option_value)
            self.__update_cfg_file()
	
	# 删除指定section
    def del_section(self, section_name):
        if self.__config.has_section(section_name):
            self.__config.remove_section(section_name)
            self.__update_cfg_file()

	# 删除指定section下的option
    def del_option(self, section_name, option_name):
        if self.__config.has_option(section_name, option_name):
            self.__config.remove_option(section_name, option_name)
            self.__update_cfg_file()

	# 更新指定section下的option的值
    def update_option_value(self,section_name, option_key, option_value):
        if self.__config.has_option(section_name,option_key):
            self.add_option(section_name, option_key, option_value)
	
	# 私有方法:操作配置文件的增删改时,更新配置文件的数据
    def __update_cfg_file(self):
        with open(self.__cfg_file, "w") as f:
            self.__config.write(f)

配置文件格式:
[database]
username = “root”
password = “root”

[driver_path]
chrome_path = “Chrome.exe”
ie_path = “IE.exe”
firefox = “FireFox.exe”

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值