python configparser.RawConfigParser.readfp(f,source=None)從文件中讀取配置

使用configparser.RawConfigParser()讀取單個配置文件,使用read()或者使用read_file()都可以實現數據的讀寫。下面就使用read_file(f, source=None)實現讀取配置數據。1、配置文件使用之前的mysql.ini文件。

mysql.ini

2、讀取文件的方法可以使用readfp(fp, fp.name),也可以使用read_file(fp, fp.name)。因爲將來readfp(fp, fp.name)將會被廢棄,所以建議使用read_file(fp, fp.name)。
read_file(f, source=None)方法中的source參數可以省略,可以寫成read_file(fp)。

# coding:utf-8
'''Note:   
讀取配置文件
Author:Qred
Date:2019/8/27
'''
import configparser
def main():
   path = 'mysql.ini'
   cfg = configparser.RawConfigParser()   
   with open(path, 'r') as fp:      
   	# cfg.readfp(fp, fp.name)      
   	cfg.read_file(fp, fp.name)
   	
   	print '獲取section節點'      
   	print cfg.sections()      
   	print '獲取section的所用配置信息'      
   	print cfg.items('db')      
   	print '獲取指定section的options即該節點的所有鍵'      
   	print cfg.options('db')     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章