
正文
Python中.ini文件使用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
.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 oscurPath = 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")






