Configparser module for operating configuration files
A configuration file (INI file) consists of a section, a key, a value
1. Config=configparser.configparser () Create Configparser instance
2. Config.sections () returns the section sequence in the configuration file
3. Config.options (section) returns a sequence of all keys in a project
4, Config.get (section,option) returns the key value of option in section
5, Config.add_section (str) Add a configuration file node (str)
6, Config.set (section,option,val) Set the value of the key named option (Val) in the section node
7. Config.read (filename) Read the configuration file
8, Config.write (obj_file) write the configuration file
Case:
Import Configparser
Import OS
Class Readwriteconffile:
Currentdir=os.path.dirname (file) @staticmethod
br/>filepath=currentdir+os.path.sep+ "Inetmsgconfigure.ini"
@staticmethod
Cf=configparser.configparser ()
Cf.read (Readwriteconffile.filepath)
return cfbr/> @staticmethod
F=open (Readwriteconffile.filepath, "w"); Br/>cf.write (f)
F.close ();
@staticmethod
Cf=readwriteconffile.getconfigparser ()
return Cf.get (section, key) br/> @staticmethod
Cf=readwriteconffile.getconfigparser ()
Allsections=cf.sections ()
If clause in Allsections:br/>return
Else
Cf.add_section (section)
Readwriteconffile.writeconfigparser (CF)
@staticmethod
Cf=readwriteconffile.getconfigparser ()
Cf.set (section, key, value)
Readwriteconffile.writeconfigparser (CF)
Ifname= = 'Main‘:
Readwriteconffile.addsection (' Messages ')
Readwriteconffile.setsectionvalue (' Messages ', ' name ', ' Sophia ')
X=readwriteconffile.getsectionvalue (' Messages ', ' 1000 ')
Python configparser module