python3 操作 .ini文件
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)
def getSectionOptionString(self, section, option, FilePath=""):
FilePath = self.IfPathLegal(FilePath)
self.ini.read(FilePath)
if self.ini:
return self.ini.get(section, option)
def getSectionOptionInt(self, section, option, FilePath=""):
FilePath = self.IfPathLegal(FilePath)
self.ini.read(FilePath)
if self.ini:
return self.ini.getint(section, option)
def getSectionOptionFloat(self, section, option, FilePath=""):
FilePath = self.IfPathLegal(FilePath)
self.ini.read(FilePath)
if self.ini:
return self.ini.getfloat(section, option)
def getSectionOptionBool(self, section, option, FilePath=""):
FilePath = self.IfPathLegal(FilePath)
self.ini.read(FilePath)
if self.ini:
return self.ini.getboolean(section, option)
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"
if os.path.exists(self.filePath):
os.remove(self.filePath)
self.ini = IniParser(self.filePath)
def section0Create(self):
self.ini.addSection(self.section0)
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)
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)
for sec in sections:
options = self.ini.getSectionOptionsNameList(sec)
print(sec, " 中的options为: ", options)
for sec in sections:
items = self.ini.getSectionItems(sec)
print(sec, " 中的配置信息为: ", items)
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()