.ini文件
一般用來配置常量或者數據庫鏈接語句等,是純文本格式,所以可以用純文本編輯器來編輯其內容。
;文件格式如下
;注釋用分號開頭,setion 節
[setion]
key = value
section不能重復,里面數據通過section去查找,每個seletion下可以有多個key和vlaue的鍵值對,注釋用英文分號(;)
例如:config.ini
[link] url=http://www.baidu.com [account] user = test password = test123
ini文件讀取
python3中自帶configparser模塊來讀取ini文件。
例如:readConfig.py
import configparser import os curPath = os.path.dirname(os.path.realpath(__file__)) cfgPath = os.path.join(curPath, "config.ini") class ReadConfig: def __init__(self): self.cf = configparser.ConfigParser() self.cf.read(cfgPath) def get_url(self): return self.cf.get("link", "url") def get_user(self): return self.cf.get("account", "user") def get_password(self): return self.cf.get("account", "password")