1 #-*-coding:utf-8-*-2 ImportConfigparser3 #Configparser Module4 5 #1. Create a Create object6 #2. Create a configuration item dictionary that creates a key-value pair7 #3. Create files to write configuration entries8 9Config =Configparser. Configparser ()Ten One #Create method One: Aconfig["DEFAULT"] = { - 'Serveraliveinternel':' $', - 'conpression':'Yes', the 'Conpressionlevel':'9' - } - - #Create method Two: +config['dabaojian.org'] = {} -config['dabaojian.org']['Host'] ='11.4.10.20' +config['dabaojian.org']['Port'] ='8080' Aconfig['dabaojian.org']['Zone'] ='Ky1' at - #Create method Three: - -config["mctianyou"] = {} -A =config["mctianyou"] -a["a"] ='m' ina["b"] ="v" -a["C"] ="op" to + - theWith open ("Testconfig.ini","W") as ConfigFile: *Config.write (ConfigFile)#write content to the created file $ Panax Notoginseng - #read the contents of the configuration file: the #Read () method + #Get a value A theConfig.read ("Testconfig.ini") + Print(Config.sections ())#can only come up with non-default modules - Print(Config.defaults ())#remove data from the default module only $g=config["DEFAULT"]["conpression"] $ Print(g) - - #a special attribute of default is as follows: the #He's going to print out the key values in the default module. - forKeyinchconfig['dabaojian.org'] :Wuyi Print(Key) the - Wu #changes to the configuration file: - #a very important feature of the file is that when the file is generated and cannot be modified, a new file must be regenerated after the contents of the file are modified to About #overwrite the original file to $ - #deletion of values in a file - #to delete a key-value pair under a configuration module -Config.remove_section ("mctianyou")#Delete a file by key value AConfig.write (Open ("Testconfig.ini","W")) +Config.remove_option ("DEFAULT","Conpressionlevel") the - #Determine if you have this key value $Flag = Config.has_section ("mctianyou") the Print(flag)#False the the #modification of configuration file values theConfig.set ("DEFAULT","conpression","No")
Configparse module of the Python module