Python has the Configparser class, which makes it easy to read data from a configuration file (such as the configuration of a db, the configuration of a path).
The format of the configuration file is: [] contains a key called section, the section has Option=value and so on
Common methods of this module
1, Config=configparser.configparser ()
Creating an Configparser instance
2, Config.sections ()
Returns a sequence of sections in a configuration file
3. Config.options (section)
Returns a sequence of all the keys in a project
4, Config.get (section,option)
Returns the key value of option in the section stanza
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)
Reading configuration Files
8, Config.write (Obj_file)
Write a configuration file
Example: Configuration file test.conf [section1]name = tankage = 28[section2]ip = 192.168.1.1port = 8080 code example: #-*-coding:utf-8-*-Import configparserconf = Configparser.configparser ()------Create an Object Conf.read ("c:\\test.conf") # Gets the specified section, The value of the specified option name = Conf.get ("Section1", "name") print (name) age = Conf.get ("Section1", "Age") print age# Gets all the section-----output as an array sections = Conf.sections () print sections# write config file # Update the value of the specified section, option Conf.set ("Section2", " Port "," 8081 ") # writes the specified section, adds the value of the new option Conf.set (" Section2 "," Ieport "," 80 ") # Add a new Sectionconf.add_section (" New_ Section ") Conf.set (" New_section "," New_option "," Http://www.cnblogs.com/tankxiao ") # Write back the profile conf.write (open (" c:\\ Test.conf "," W "))-------remember the changes to the configuration file and must be written to the configuration file via the Write method, otherwise invalid
Configuration file parsing module-configparser in Python