python config檔案的讀寫操作示例
阿新 • • 發佈:2020-01-09
本文例項講述了python config檔案的讀寫操作。分享給大家供大家參考,具體如下:
1、設定配置檔案
[mysql] host = 1234 port = 3306 user = root password = Zhsy08241128 database = leartd
2、讀取配置檔案
import configparser import os conf= configparser.ConfigParser() def readConf(): '''讀取配置檔案''' root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) conf.read(root_path + '/ceshi/conf/app.conf') # 檔案路徑 print(conf) name = conf.get("mysql","host") # 獲取指定section 的option值 print(name)
3、寫入配置檔案
def writeConf(): '''寫入配置檔案''' root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) conf.read(root_path + '/ceshi/conf/app.conf') # 檔案路徑 conf.set("mysql","host","1234") # 修改指定section 的option conf.write(open(root_path + '/ceshi/conf/app.conf','w'))
更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python函式使用技巧總結》、《Python面向物件程式設計入門與進階教程》、《Python資料結構與演算法教程》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總》
希望本文所述對大家Python程式設計有所幫助。