打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python操作ini配置文件代码

ixamail.ini

[host]
smtp_server=smtp.126.com
smtp_username=aspphp
smtp_password=123456
[task]
task_url=http://126.com

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('ixamail.ini))
a = config.get("host","smtp_server")
print a

写文件也简单

import ConfigParser


config =
ConfigParser.ConfigParser()

# set a number of parameters

config.add_section("book")
config.set(
"book", "title", "the python standard library"
)
config.set(
"book", "author", "fredrik lundh"
)

config.add_section(
"ematter"
)
config.set(
"ematter", "pages", 250
)

# write to file

config.write(open('1.ini', "w"))

修改也可以

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import ConfigParser

config
=
ConfigParser.ConfigParser()

config.read(
'1.ini'
)

a
= config.add_section("md5"
)

config.set(
"md5", "value", "1234"
)

config.write(open(
'1.ini', "r+"
)) #可以把r+改成其他方式,看看结果:)

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import ConfigParser

config
=
ConfigParser.ConfigParser()

config.read(
'1.ini'
)

config.set(
"md5", "value", "kingsoft"
) #这样md5就从1234变成kingsoft了

config.write(open(
'1.ini', "r+"))

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python3:读取配置dbconfig.ini(含有中文)显示乱码的解决方法
Python常用配置文件ini、json、yaml读写总结
[快学Python3]INI文件读写
那些在 Python 3 中闪亮的
python笔记15-ini配置文件(configparser)
5 分钟掌握 Python 中常见的配置文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服