【Python】ConfigParser模組
一 前言
最近研究備份恢復MySQL資料庫例項,老的資料配置和新的例項的my.cnf 配置不統一,依賴backup-my.cnf 來判斷innodb_data_file_path 引數是否修改修改。如何解析 my.cnf 呢?於是研究了Python提供ConfigParser模組。該模組可以完成針對常見的配置檔案的讀取和修改操作,基本滿足需求。
二 如何使用
2.1 配置檔案的格式
配置檔案主要由 section區域 構成,section中可以使用option=value或option:value,來配置引數。
常見的 my.cnf 格式 如下
2.2 ConfigParser 模組
Python的ConfigParser Module定義了3個類:RawCnfigParser,ConfigParser,SafeConfigParser. 其中RawCnfigParser 是最基礎的配置檔案讀取類,ConfigParser、SafeConfigParser基於 RawCnfigParser做了各自的擴充
本文主要以ConfigParser類為例做介紹。ConfigParser模組的操作主要包括:
a 初始化一個 ConfigParser例項
b 讀取配置
c 修改配置
讀取配置檔案常用的方法
修改配置檔案常用的方法
2.3 例子
yangyiDBA:test yangyi$ python writecnf.py
三 小結
根據ConfigParser 模組提供的函式,基本可以滿足日常工作中對配置檔案的修改操作。其他更詳細的資料請參考。
最近研究備份恢復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
-
[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
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將內容寫入配置檔案。
點選(此處)摺疊或開啟
-
#!/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
-
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 模組提供的函式,基本可以滿足日常工作中對配置檔案的修改操作。其他更詳細的資料請參考。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-2128208/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python模組之configparserPython
- Python學習——configparser模組Python
- python常用模組補充hashlib configparser logging,subprocess模組Python
- Python3 中 configparser 模組解析配置的用法詳解Python
- Python的configparser模組讀取.ini檔案內容並輸出Python
- python 模組:itsdangerous 模組Python
- Python模組:time模組Python
- Python模組之urllib模組Python
- python模組之collections模組Python
- Python 模組Python
- [Python模組學習] glob模組Python
- Python中模組是什麼?Python有哪些模組?Python
- Python Execl模組Python
- Python mongoHelper模組PythonGo
- Python——JSON 模組PythonJSON
- [Python] pipe模組Python
- Python - 模組包Python
- python——typing模組Python
- Python functools 模組Python
- Python pymsql模組PythonSQL
- Python:requests模組Python
- Python模組reloadPython
- python之模組Python
- 15 Python模組Python
- python–inspect模組Python
- python random模組Pythonrandom
- python Subprocess 模組Python
- Python:pathlib模組Python
- python APScheduler模組Python
- Python webargs 模組PythonWeb
- Python模組(module)Python
- Python-模組Python
- python collections模組Python
- python 模組fnmatchPython
- python–模組之random隨機數模組Pythonrandom隨機
- python–模組之os操作檔案模組Python
- Python基礎12(模組與datetime模組)Python
- Python 使用ConfigParser操作ini配置檔案教程。Python
- Python常用模組(random隨機模組&json序列化模組)Pythonrandom隨機JSON