Python Module-configparse module

Source: Internet
Author: User

The Configparse module is used to parse the configuration file

Configuration file

[default]port= 3306socket=/tmp/mysql.sock[mysqldump]max_allowed_packet = 16m[myisamchk]key_buffer_size = 256Msort_ Buffer_size = 256mread_buffer = 2mwrite_buffer = 2M

Reading the parsing configuration file

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import configparser config = Configparser. Configparser () config.read (' Config.ini ') is #读取配置文件 and assigned to the Config print (config.sections ())  #读取配置文件中sections的标题, However, the default Sectionprint (config.default_section) #读取默认的section print (' myisamchk ' in config) #判断section是否在配置文件里 is not read, returning the Boolean type Print (List (config[' Myisamchk '].keys ())) #读取指定section和默认section里的option print (config[' myisamchk ' [' Read_buffer ']) #获取section里option的值 print (' Read_buffer ' in config[' Myisamchk ') #判断option是否在section中 # Gets the value of option and option in the specified section and default section for K,v in config[' Myisamchk '].items ():    print (K,V)

Run results

Delete actions such as adding modifications

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import configparserconfig = Configparser. Configparser () config.read (' Config.ini ') print (config.has_section (' Python ')) #判断section是否在配置文件中config. add_section (' Python ') #添加section到配置文件中config. Set (' Python ', ' abc ', ' 123c ') #给section中的option设置值 if no config.remove_option will be created (' Python ', ' abc ') #删除optionconfig. remove_section (' python ') #删除sectionconfig. Write (Open (' Config_1.ini ', "W")) # To be re-written to successfully complete the operation

Python Module-configparse module

Related Article

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.