简介
python 中, .ini .conf 等配置文件的读取/写入. 简单了解, 等有深入使用, 再补充
ini 格式
值的类型均为文本string, 即便看起来为false的值
addressee = , 意为 addressee 的值为空
值的前后, 不需要引号.
[SUBSCRIBE]switch = falseaddressee =
[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes
[SERVER_EMAIL]sender = dongyadi111@qq.comsmtp_server = smtp.qq.comuser_name = 597852076password = xuhnznimedrjbcffport = 465authentication = truessl = true
复制代码
初始化
from configparser import ConfigParserconfig = ConfigParser()config.read('/var/mail.ini')
复制代码
取值
# return test@test.comconfig['SERVER_EMAIL']['sender']config['SERVER_EMAIL'].get('senders', 'sender')config.get('SERVER_EMAIL', 'sender', fallback='fallback')# 一定记得加section!
复制代码
conf 文件取出的值, 类型均为 string. 如果想要int bool ,需要使用getint() getboolean()
写入
config = ConfigParser()config.read('/example.conf')config.set('SUBSCRIBE', 'switch', str(switch))config.set('SUBSCRIBE', 'addressee', str(addressee))with open(HUNTER_SUBSCRIBE_FILE, 'w') as configfile: config.write(configfile)
复制代码
应用
python SendEmail 存取邮件配置
参考
官网文档
评论