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 exists2. 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)