读写配置文件在实际应用中具有十分强大的功能,python 读写配置文件ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件。
比如配置文件内容如下:
[EMAIL]
mail_host = smtp
.exmail.qq.com
mail_user = xiaoxi@xyd
.cn
其中[ ] 中的EMAIL是这段配置的名字下面mail_host ,mail_user 都是属性
python主要代码:
import ConfigParser
config=ConfigParser.ConfigParser()
with codecs.
open(
'config.ini',
'w')
as conf:
config.readfp(conf)
mail_host =config.
get(
'EMAIL',
'mail_host')
mail_user =config.
get(
'EMAIL',
'mail_user')
print mail_host
print mail_user
config.
set(
'EMAIL',
'mail_pass',
'male')
config.
set(
'EMAIL',
'mail_port',
'25')
也可以自己封装一下,怎么简便怎么来嘛 配置文件:
python脚本:
转载请注明原文地址: https://ju.6miu.com/read-12037.html