記錄乙個讀取my.cnf配置的指令碼,可以基於該函式做一些mysql 後端運維工作。各位可以基於自己的需求進行修改。
指令碼名稱 getcnf.py
import sys
import os
def read_cnf(cnf_path):
assert cnf_path is not none and os.path.exists(cnf_path)
cnf_dict = {}
cur_section = none
with open(cnf_path) as cnf_reader:
for line in cnf_reader.readlines():
line = ''.join(line.split())
if len(line) <= 0 or '#' == line[0]:
continue
if '[' == line[0] and ']' == line[-1]:
cur_section = line[len('['):len(line) - 1]
if cur_section not in cnf_dict:
cnf_dict[cur_section] = {}
elif '=' in line and line.count('=') == 1:
if cur_section is none:
logger.warning('cur_section is none')
continue
tokens = line.split('=')
key = tokens[0].replace('"', '').replace("'", '')
value = tokens[1].replace('"', '').replace("'", "")
cnf_dict[cur_section][key] = value
return cnf_dict
def main():
cnf_path="/u01/my3353/my.cnf"
mycnf = read_cnf(cnf_path)
print mycnf['mysqld']['tmpdir']
if __name__ == '__main__':
main()
執行效果圖 python讀取配置 python讀取配置檔案
本機 python3.5 1.需要安裝configerparser py2是 configparser 2.編寫 執行前 coding utf8 import configparser conf configparser.configparser conf.read f python clstudy...
python讀取properties配置檔案並解析
ld.properties 配置資訊 database.type argo database.name dd database.ip 10.12.15.225 database.port 10000 database.username user database.password passwd pr...
python 讀取mysql資料
import pymysql import pandas as pd defload data from mysql conn pymysql.connect host 127.0.0.1 port 3306 user test password test db database charset u...