Configparser module One, create a configuration file in the D disk to establish a configuration file, the name is: Test.ini content as follows: [Baseconf]host=127.0.0.1port=3306user=rootpassword=rootdb_ Name=gloryroad[test]ip=127.0.0.1int=1float=1.5bool=true Note: To save the file as ANSI encoding, the UTF-8 encoding will error in the file [baseconf] is section Second, read the configuration file Import Configparsercf=configparser.configparser () cf.read (path) Read configuration file (INI, conf) Returns the result is a list cf.sections () Gets all sections (domains) read, returns all keys under a domain of list type cf.options (' sectionname '), returns all keys under a domain of list type cf.items (' sectionname '), Value for Value=cf.get (' sectionname ', ' key ') obtains the value value of a key under one of Yu's Values Cf.type (value) for the type (1) Getint (section, option) Gets the value of option in section and returns the int type data, so the function can only read values of type int. (2) Getboolean (section, option) Gets the value of option in section and returns the Boolean type data, so the function can only read Boolean values. (3) GetFloat (section, option) Gets the value of option in section and returns floating-point type data, so the function can only read values of floating-point types. (4) Has_option (section, option) detects whether the specified option exists under the specified section, or returns False if there is a return of true. (5) Has_section (section) detects if the specified sections exist in the configuration file and returns False if there is true. Three, dynamic write profile cf.add_section (' Test ') add a domain cf.set (' test3 ', ' key12 ', ' value12 ') to add a key value to Cf.write (open (Path, 'W ')) to use ' W '
Python configuration file read/write