Python configuration file read/write

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.