[Python] ConfigParser module

Source: Internet
Author: User
I. Preface & amp; nbsp; recently, I have studied how to back up and restore MySQL database instances, old data configurations, and my. the cnf configuration is not uniform, depending on the backup-my.cnf to determine whether the innodb_data_file_path parameter is modified or not. How to resolve my Preface
Recent research backup recovery MySQL database instance, old data configuration and new instance my. cnf configuration is not uniform, rely on the backup-my.cnf to determine whether innodb_data_file_path parameter modification. How to resolve my. cnf? So I studied how to provide the ConfigParser module in Python. This module can read and modify common configuration files to meet the requirements.
II. how to use
2.1 configuration file format
The configuration file consists of the section area. you can use option = value or option: value in the section to configure parameters.

[Section1 name]

Option1 = value 1

....

OptionN = value N

[Section2 name]

Option1 = value 1

....

OptionN = value N

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 three classes: RawCnfigParser, ConfigParser, and SafeConfigParser. RawCnfigParser is the most basic configuration file reading class, and ConfigParser and SafeConfigParser are expanded based on RawCnfigParser.
This article mainly introduces the ConfigParser class as an example. The ConfigParser module provides the following operations:
A initializes a ConfigParser instance.
B. Read configuration
C. modify configuration
Common methods for reading configuration files

Cf. read (filename) reads the configuration file content

Cf. sections () obtains all sections and returns them as a list.

Cf. options (section): obtain all options under a specified section.

Cf. items (section) obtains all key-value pairs under a specified section and returns them as tuples.

Cf. get (section, option) gets the option value in the specified section and returns the string type

Cf. getint (section, option) gets the option value in the specified section and returns the int type.

Cf. has_option (section, option) check whether a specified option exists in the section. If yes, True is returned. If no option exists, False is returned.

Cf. has_section (section) check whether there are sections. If yes, True is returned. If no, False is returned.

Common methods for modifying configuration files

Cf. add_section (section) add a new section to the configuration file.

Cf. set (section, option, value) to set options in section

Cf. remove_section (section) delete the specified section

Cf. remove_option (section, option) delete option in a specified section

Note: for the configuration file modification operation, you must call write to write the content to the configuration file.

2.3 Example

Click (here) to fold 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 "modified"

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

YangyiDBA: 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']

('Innodb _ log_files_in_group ', '2 ')

('Innodb _ page_size ', '123 ')

('Innodb _ log_block_size ', '123 ')

('Innodb _ data_file_path ', 'ibdata1: 2G: autoextend ')

('Innodb _ log_file_size ', '123 ')

('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

Summary
According to the functions provided by the ConfigParser module, the configuration file can be modified in daily work. For more details, see the official documentation.

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.