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