Python Learning Note 21 (read config file)

Source: Internet
Author: User

1. Basic read operation
    • -read (filename )                 read file contents directly
    • -sections ()                         get all the sections and return as a list
    • -options (section)              Get all option
    • -items (section)    for the section;                gets all the key values for the section to
    • -get (section,option)          Gets the value of option in section, returned as String type
    • -getint (section,option)     Gets the value of option in section, returns the int type, and the corresponding Getboolean () and GetFloat () functions.
# Peizhi.ini  = = 192.168.1.1= 8080
ImportConfigparser#introduce the necessary packagesCF= Configparser. Configparser ()#instantiating a Configparser objectCf.read ("Peizhi.ini")#reading configuration Filess= Cf.sections ()#Read all the sectionsi = Cf.items ('section2222')#Read all key-value pairs under this sectiono = Cf.options ('section2222')#Read all the option under the section, that is, the keyK = Cf.get ('section2222','IP')#gets the value of the option in the sectionHas_sec = Cf.has_option ('section2222','name')#determines whether the key value pair exists
2. Basic write operation
    • -write (FP) writes the Config object to a. init format file , write a. Ini-format representation of the configuration state.
    • -add_section (section) Add a new section
    • -set (section, option, value sets the option in the section and needs to call write to write the content to the configuration file
    • -remove_section (section) Delete a section
    • -remove_option (section, option)

Python Learning Note 21 (read config file)

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.