码迷,mamicode.com
首页 > 编程语言 > 详细

python读写修改配置文件(ini)

时间:2019-04-13 18:53:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:文件   standard   and   import   auth   param   ges   读取配置   color   

python 有时候参数需要保存到配置文件中  接下来总结一下 配置文件的读写和修改的操作

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 读取配置文件
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open(update.ini))
a = config.get("ZIP","MD5")
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")) # 没有新建  存在打开

# 修改配置文件内容
fixConfig = ConfigParser.ConfigParser()
fixConfig.read(1.ini)
a = fixConfig.add_section("md5")
fixConfig.set("md5", "value", "1234")
fixConfig.write(open(1.ini, "r+")) #可以把r+改成其他方式,看看结果:)

运行结果:

aaeb519d3f276b810d46642d782d8921

python读写修改配置文件(ini)

标签:文件   standard   and   import   auth   param   ges   读取配置   color   

原文地址:https://www.cnblogs.com/wanghuixi/p/10702371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!