This example describes the Python read-write INI configuration file method. Share to everyone for your reference. The implementation method is as follows:
Import Configparserimport osclass readwriteconffile:currentdir=os.path.dirname (__file__) filepath=currentDir+ os.path.sep+ "Inetmsgconfigure.ini" @staticmethod def getconfigparser (): Cf=configparser.configparser () Cf.read (Rea Dwriteconffile.filepath) return CF @staticmethod def writeconfigparser (CF): F=open (Readwriteconffile.filepath, "w") ; Cf.write (f) f.close (); @staticmethod def getsectionvalue (section,key): Cf=readwriteconffile.getconfigparser () return Cf.get (section, key) @staticmethod def addsection (section): Cf=readwriteconffile.getconfigparser () allsections=cf.sections () if sect Ion in Allsections:return else:cf.add_section (section) readwriteconffile.writeconfigparser (CF) @stati Cmethod def setsectionvalue (section,key,value): Cf=readwriteconffile.getconfigparser () cf.set (section, key, value) Readwriteconffile.writeconfigparser (CF) if __name__ = = ' __main__ ': readwriteconffile.addsection (' Messages ') ReadWriteconffile.setsectionvalue (' Messages ', ' name ', ' Sophia ') x=readwriteconffile.getsectionvalue (' Messages ', ' 1000 ') ) Print X
Hopefully this article will help you with Python programming.