python3操作 .ini文件

python3 操作 .ini文件

# -*- coding:utf-8 -*-

from configparser import ConfigParser
import os


class IniParser:
    def __init__(self, FliePath):
        self.FliePath = FliePath
        self.ini = ConfigParser()

    def IfPathLegal(self, FliePath):
        if FliePath == "":
            FliePath = self.FliePath
        return FliePath

    # 获取所有节点名称列表
    def getSectionsNameList(self, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.sections()

    # 获取指定节点所有选项名称列表
    def getSectionOptionsNameList(self, section, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.options(section)

    # 获取指定节点的所有选项值列表
    def getSectionItems(self, section, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.items(section)


    # region 获取指定类型数据
    # 返回字符串类型
    def getSectionOptionString(self, section, option, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.get(section, option)

    # 返回int类型
    def getSectionOptionInt(self, section, option, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.getint(section, option)

    # 返回float类型
    def getSectionOptionFloat(self, section, option, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.getfloat(section, option)

    # 返回bool类型
    def getSectionOptionBool(self, section, option, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        self.ini.read(FilePath)
        if self.ini:
            return self.ini.getboolean(section, option)
    # endregion


    # 新增节点
    def addSection(self, section, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        if self.ini:
            self.ini.add_section(section)
            self.ini.write(open(FilePath, "w"))

    # 设置指定节点选项的值
    def setSectionOptionValue(self, section, option, value, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        if self.ini:
            self.ini.set(section, option, value)
            self.ini.write(open(FilePath, "w"))

    # 删除指定节点
    def delSection(self, section, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        if self.ini:
            self.ini.remove_section(section)
            self.ini.write(open(FilePath, "w"))

    # 删除指定节点选项
    def delSectionOption(self, section, option, FilePath=""):
        FilePath = self.IfPathLegal(FilePath)
        if self.ini:
            self.ini.remove_option(section, option)
            self.ini.write(open(FilePath, "w"))


if __name__ == "__main__":
    print("python ini实例")

    class test():
        # 初始化信息
        def __init__(self):
            self.filePath = "./ini.ini"
            self.section0 = "section0"  # 节点
            self.section1 = "section1"  # 节点

            # 如果存在mysql.ini先删除,方便下列代码的测试
            if os.path.exists(self.filePath):
                os.remove(self.filePath)

            # 也可以在每个方法调用时传入文件名
            self.ini = IniParser(self.filePath)

        # 添加文件内容测试
        def section0Create(self):
            self.ini.addSection(self.section0)
            # 在mysql section下写入一些配置信息
            self.ini.setSectionOptionValue(self.section0, "host", "127.0.0.1")
            self.ini.setSectionOptionValue(self.section0, "port", "3306")
            self.ini.setSectionOptionValue(self.section0, "db", "autotesting")
            self.ini.setSectionOptionValue(self.section0, "user", "root")
            self.ini.setSectionOptionValue(self.section0, "password", "123456")

        def section1Create(self):
            self.ini.addSection(self.section1)
            # oracle section下写入一些配置信息
            self.ini.setSectionOptionValue(self.section1, "host", "127.0.0.1")
            self.ini.setSectionOptionValue(self.section1, "port", "1520")
            self.ini.setSectionOptionValue(self.section1, "db", "auto_ui")
            self.ini.setSectionOptionValue(self.section1, "user", "sa")
            self.ini.setSectionOptionValue(self.section1, "password", "123456")

        # 读取文件
        def readFile(self):
            sections = self.ini.getSectionsNameList()
            print(sections)

            # 遍历各个section下的options,并在console中输出
            for sec in sections:
                options = self.ini.getSectionOptionsNameList(sec)
                print(sec, " 中的options为: ", options)

            # 获取各个section下的配置信息
            for sec in sections:
                items = self.ini.getSectionItems(sec)
                print(sec, " 中的配置信息为: ", items)

            # 获取指定的option值这里演示读取host和port
            host = self.ini.getSectionOptionString(self.section0, "host")
            port = self.ini.getSectionOptionInt(self.section0, "port")
            print(host,"类型:",type(host),"||", port,"类型:" ,type(port))

        # 修改文件
        def alterFile(self):
            self.ini.delSectionOption(self.section0, "host")
            self.ini.delSection(self.section1)
            self.ini.setSectionOptionValue(self.section0, "port", "4000")

    win = test()
    win.section0Create()
    win.section1Create()
    win.readFile()
    win.alterFile()



发布了34 篇原创文章 · 获赞 54 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42686768/article/details/103415940