Home > Backend Development > PHP Tutorial > [Python]ConfigParser module

[Python]ConfigParser module

巴扎黑
Release: 2016-11-21 15:30:55
Original
1022 people have browsed it

1 Preface
Recently, I have been studying the backup and restoration of MySQL database instances. The old data configuration and the my.cnf configuration of the new instance are not consistent. I rely on backup-my.cnf to determine whether the innodb_data_file_path parameter has been modified. How to parse my.cnf? So I studied the ConfigParser module provided by Python. This module can complete reading and modifying operations for common configuration files, basically meeting the needs.
2 How to use
2.1 Format of the configuration file
The configuration file is mainly composed of the section area. You can use option=value or option:value in the section to configure parameters.

[section1 name]

option1=value1

....

optionN=valueN

[section2 name]

option1=value1

....

optionN=valueN

The common my.cnf format is as follows

[mysqld]

innodb_log_files_in_group = 2

innodb_page_size = 16384

innodb_log_block_size = 512

innodb_data_file_path = ibdata1:2G:autoextend

innodb_log_file_size = 536870912

2.2 ConfigParser Module
Python’s ConfigParser Module defines 3 classes: RawCnfigParser, ConfigParser, SafeConfigParser. Among them, RawCnfigParser is the most basic configuration file reading class, ConfigParser, SafeConf igParser has made its own extensions based on RawCnfigParser
This article mainly uses the ConfigParser class as an example. The operations of the ConfigParser module mainly include:
a Initialize a ConfigParser instance
b Read the configuration
c Modify the configuration
Common methods for reading configuration files

cf.read(filename) Read the content of the configuration file

cf. sections() Get all sections and return them in the form of a list

cf.options(section) Get all options under the specified section

cf.items(section) Get all key-value pairs under the specified section, as tuples Returns in the form

cf.get(section,option) Gets the value of option in the specified section and returns it as string type

cf.getint(section,option) Gets the value of option in the specified section and returns it as int type

cf.has_option(section,option) Check whether there is a specified option under the section. If it returns True, if not, it returns False.

cf.has_section(section) Check if there is a section. If it returns True, if it does not return False

Modification Commonly used methods for configuration files

cf.add_section(section) Add a new section to the configuration file

cf.set(section, option, value) Set the options in the section

cf.remove_section( section) Delete the specified section

cf.remove_option(section,option) Delete the option in the specified section

Note that to modify the configuration file, you need to call write to write the content into the configuration file.

2.3 Example

Click (here) to collapse or open

#!/usr/bin/python2.6

#coding:utf8

import ConfigParser

old_mycnf_file='backup-my.cnf '

new_mycnf_file='my.cnf'

cf =ConfigParser.ConfigParser()

cf.read(new_mycnf_file)

sec=cf.sections()

print 'sections:' ,sec

opts = cf.options("mysqld")

print 'options:', opts

kvs = cf.items("mysqld")

for kv in kvs:

print kv

innodb_data_file_path=cf.get('mysqld','innodb_data_file_path')

innodb_log_file_size=cf.get('mysqld','innodb_log_file_size')

print 'innodb_data_file_path :',innodb _data_file_path

print 'innodb_log_file_size: ',innodb_log_file_size

print "After modification"

cf.set('mysqld','innodb_data_file_path','ibdata1:1G:autoextend')

cf.write(open(new_mycnf_file, "w"))

cf.read(new_mycnf_file)

innodb_data_file_path=cf.get('mysqld','innodb_data_file_path')

print 'innodb_data_file_path :',innodb_data_file_path

yangyiD BA:test yangyi$ python writecnf.py

sections: ['mysqld']

options: ['innodb_log_files_in_group', 'innodb_page_size', 'innodb_log_block_size', 'innodb_data_file_path', 'innodb_log_file_size', 'ibdata1']

('inno db_log_files_in_group', '2')

('innodb_page_size', '16384')

('innodb_log_block_size', '512')

('innodb_data_file_path', 'ibdata1:2G:autoextend')

('innod b_log_file_size', '536870912' )

('ibdata1', '2g:autoextend = ibdata1:2G:autoextend')

innodb_data_file_path : ibdata1:1G:autoextend

innodb_log_file_size : 536870912

After modification

innodb_data_file_path : ibdata1:1G:autoextend

Three Summary
Based on the functions provided by the ConfigParser module, it can basically meet the needs of modifying configuration files in daily work. For more detailed information, please refer to the official documentation.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template