【Python】ConfigParser模組

楊奇龍發表於2016-11-10
一 前言
   最近研究備份恢復MySQL資料庫例項,老的資料配置和新的例項的my.cnf 配置不統一,依賴backup-my.cnf 來判斷innodb_data_file_path 引數是否修改修改。如何解析 my.cnf 呢?於是研究了Python提供ConfigParser模組。該模組可以完成針對常見的配置檔案的讀取和修改操作,基本滿足需求。
二 如何使用
  2.1 配置檔案的格式
  配置檔案主要由 section區域 構成,section中可以使用option=value或option:value,來配置引數。 
  1. [section1 名稱]
  2. option1=值1
  3. ....
  4. optionN=值N
  5. [section2 名稱]
  6. option1=值1
  7. ....
  8. optionN=值N
  常見的 my.cnf 格式 如下
  1. [mysqld]
  2. innodb_log_files_in_group = 2
  3. innodb_page_size = 16384
  4. innodb_log_block_size = 512
  5. innodb_data_file_path = ibdata1:2G:autoextend
  6. 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 修改配置
讀取配置檔案常用的方法

  1. cf.read(filename)   讀取配置檔案內容
  2. cf.sections()       獲取所有的section,並以列表的形式返回
  3. cf.options(section) 獲取指定section下所有option
  4. cf.items(section)   獲取指定section下所有鍵值對,以元組的形式返回
  5. cf.get(section,option) 獲取指定section中option的值,返回為string型別
  6. cf.getint(section,option) 獲取指定section中option的值,返回為int型別
  7. cf.has_option(section,option) 檢查section下是否有指定的option,有返回True,無返回 False
  8. cf.has_section(section) 檢查是否有section,有返回True,無返回 False
修改配置檔案常用的方法
  1. cf.add_section(section) 向配置檔案中新增一個新的section
  2. cf.set(section,option,value) 對section中的option進行設定
  3. cf.remove_section(section) 刪除指定的section
  4. cf.remove_option(section,option) 刪除指定section中的option
  5. 注意對於修改配置檔案的操作需要呼叫write將內容寫入配置檔案。
2.3 例子

點選(此處)摺疊或開啟

  1. #!/usr/bin/python2.6
  2. #coding:utf8
  3. import ConfigParser
  4. old_mycnf_file='backup-my.cnf'
  5. new_mycnf_file='my.cnf'
  6. cf =ConfigParser.ConfigParser()
  7. cf.read(new_mycnf_file)
  8. sec=cf.sections()
  9. print 'sections:' ,sec
  10. opts = cf.options("mysqld")
  11. print 'options:', opts
  12. kvs = cf.items("mysqld")
  13. for kv in kvs:
  14.     print kv
  15. innodb_data_file_path=cf.get('mysqld','innodb_data_file_path')
  16. innodb_log_file_size=cf.get('mysqld','innodb_log_file_size')
  17. print 'innodb_data_file_path :',innodb_data_file_path
  18. print 'innodb_log_file_size :',innodb_log_file_size
  19. print "修改之後"
  20. cf.set('mysqld','innodb_data_file_path','ibdata1:1G:autoextend')
  21. cf.write(open(new_mycnf_file, "w"))
  22. cf.read(new_mycnf_file)
  23. innodb_data_file_path=cf.get('mysqld','innodb_data_file_path')
  24. print 'innodb_data_file_path :',innodb_data_file_path
yangyiDBA:test yangyi$ python writecnf.py 
  1. sections: ['mysqld']
  2. options: ['innodb_log_files_in_group', 'innodb_page_size', 'innodb_log_block_size', 'innodb_data_file_path', 'innodb_log_file_size', 'ibdata1']
  3. ('innodb_log_files_in_group', '2')
  4. ('innodb_page_size', '16384')
  5. ('innodb_log_block_size', '512')
  6. ('innodb_data_file_path', 'ibdata1:2G:autoextend')
  7. ('innodb_log_file_size', '536870912')
  8. ('ibdata1', '2g:autoextend = ibdata1:2G:autoextend')
  9. innodb_data_file_path : ibdata1:1G:autoextend
  10. innodb_log_file_size : 536870912
  11. 修改之後
  12. innodb_data_file_path : ibdata1:1G:autoextend
三 小結
   根據ConfigParser 模組提供的函式,基本可以滿足日常工作中對配置檔案的修改操作。其他更詳細的資料請參考。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-2128208/,如需轉載,請註明出處,否則將追究法律責任。

相關文章