Heim  >  Artikel  >  Backend-Entwicklung  >  【Python】ConfigParser模块

【Python】ConfigParser模块

巴扎黑
巴扎黑Original
2016-11-21 15:30:55936Durchsuche

一 前言
   最近研究备份恢复MySQL数据库实例,老的数据配置和新的实例的my.cnf 配置不统一,依赖backup-my.cnf 来判断innodb_data_file_path 参数是否修改修改。如何解析 my.cnf 呢?于是研究了Python提供ConfigParser模块。该模块可以完成针对常见的配置文件的读取和修改操作,基本满足需求。
二 如何使用
  2.1 配置文件的格式
  配置文件主要由 section区域 构成,section中可以使用option=value或option:value,来配置参数。 

[section1 名称]

option1=值1

....

optionN=值N

[section2 名称]

option1=值1

....

optionN=值N

  常见的 my.cnf 格式 如下

[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 模块
  Python的ConfigParser Module定义了3个类:RawCnfigParser,ConfigParser,SafeConfigParser. 其中RawCnfigParser 是最基础的配置文件读取类,ConfigParser、SafeConfigParser基于 RawCnfigParser做了各自的拓展
  本文主要以ConfigParser类为例做介绍。ConfigParser模块的操作主要包括:
   a 初始化一个 ConfigParser实例
   b 读取配置
   c 修改配置
读取配置文件常用的方法

cf.read(filename)   读取配置文件内容

cf.sections()       获取所有的section,并以列表的形式返回

cf.options(section) 获取指定section下所有option

cf.items(section)   获取指定section下所有键值对,以元组的形式返回

cf.get(section,option) 获取指定section中option的值,返回为string类型

cf.getint(section,option) 获取指定section中option的值,返回为int类型

cf.has_option(section,option) 检查section下是否有指定的option,有返回True,无返回 False 

cf.has_section(section) 检查是否有section,有返回True,无返回 False

修改配置文件常用的方法

cf.add_section(section) 向配置文件中添加一个新的section 

cf.set(section,option,value) 对section中的option进行设置

cf.remove_section(section) 删除指定的section

cf.remove_option(section,option) 删除指定section中的option 

注意对于修改配置文件的操作需要调用write将内容写入配置文件。

2.3 例子

点击(此处)折叠或打开

#!/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 "修改之后"

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', '16384')

('innodb_log_block_size', '512')

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

('innodb_log_file_size', '536870912')

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

innodb_data_file_path : ibdata1:1G:autoextend

innodb_log_file_size : 536870912

修改之后

innodb_data_file_path : ibdata1:1G:autoextend

三 小结
   根据ConfigParser 模块提供的函数,基本可以满足日常工作中对配置文件的修改操作。其他更详细的资料请参考官方文档。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn