#coding:utf-8

import ConfigParser

class Conf():

def __init__(self,name):
self.name = name
self.cp = ConfigParser.ConfigParser()
self.cp.read(name)

def getSections(self):
return self.cp.sections()

def getOptions(self, section):
if self.cp.has_section(section):
return self.cp.options(section)

def getItems(self, section):
if self.cp.has_section(section):
return self.cp.items(section)

def getValue(self, section, option):
if self.cp.has_option(section, option):
return self.cp.get(section, option)

def setSection(self, section):
if not self.cp.has_section(section):
self.cp.add_section(section)
self.cp.write(open(self.name,'w'))

def setValue(self, section, option, value):
if not self.cp.has_option(section, option):
self.cp.set(section, option, value)
self.cp.write(open(self.name,'w'))

def delSection(self, section):
if self.cp.has_section(section):
self.cp.remove_section(section)
self.cp.write(open(self.name,'w'))

def delOption(self, section, option):
if self.cp.has_option(section, option):
self.cp.remove_option(section, option)
self.cp.write(open(self.name,'w'))

def updateValue(self, section, option, value):
if self.cp.has_option(section, option):
self.cp.set(section, option, value)
self.cp.write(open(self.name,'w'))

if __name__ == "__main__":
conf = Conf("confx.ini")
conf.setSection("add")
conf.setValue("add", "version", "v1.0")
conf.updateValue("add", "version", "v1.1")

print conf.getItems("add")
print conf.getSections()
conf.delSection("add")

#-----------------conf.ini--------------------
#[db]
#db_host = 127.0.0.1
#db_port = 3306
#db_user = root
#db_pass = wells
#
#[concurrent]
#thread = 10
#processor = 20

python读写配置文件的更多相关文章

  1. python 读写配置文件

    使用python读写配置文件,写个demo测试一下. #!/usr/bin/env python import os import ConfigParser # 如果文件不存在,就穿件文件. if o ...

  2. python读写配置文件使用总结与避坑指南

    关于今天的内容 最近拿python在写项目部署的相关集成代码,本来两天的工作量,硬是在来回的需求变更中,拖到了一周的时间.今天算是暂时告一段落了.这次由于涉及多个系统的调用和配置参数,代码开发中出现了 ...

  3. Python读写配置文件模块--Configobj

    一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...

  4. python-ConfigParser模块【读写配置文件】

    对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...

  5. 【python】配置文件

    来源:http://developer.51cto.com/art/201003/189885.htm python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案, ...

  6. Python-通过configparser读写配置文件

    Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...

  7. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  8. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  9. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

随机推荐

  1. (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ

    1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...

  2. Poj 2583 Series Determination

    1.Link: http://poj.org/problem?id=2583 2.Content: Series Determination Time Limit: 1000MS   Memory L ...

  3. 如何禁止KnockoutJs在VS2012的智能格式化

    http://blogs.msdn.com/b/webdev/archive/2013/03/04/disabling-knockout-intellisense.aspx 我升级了一下VS2012, ...

  4. 7.JAVA_SE复习(文件)

    文件和流 1.什么是节点流和处理流 InputStream & OutputStream Reader & Writer 乃节点流, 前面加File之类的名词 的节点流 其余加动词的均 ...

  5. (转)jquery ajax使用及跨域访问解决办法

    原文地址:***/UIweb/jquery_ajax_kuayujiejue.html 最近开发中,设计到智能手机项目,给领导做几个demo.主要是用jquery和jqeury mobile. 越来越 ...

  6. mongodb 级联操作查询时,关联条件

    ObjectId id=new ObjectId("123"); c=c.where("relation_type.$id").is(id);

  7. ACE_linux:TCP通信

    1.涉及类 ACE_INET_Addr//ACE网络地址ACE_SOCK_Acceptor//ACE网络服务器ACE_SOCK_Connector//ACE网络客户端ACE_SOCK_Stream// ...

  8. [Database][SQL] 取得SQLServer中某一欄位名稱所在的資料表及欄位相關資訊

    取得SQLServer中某一欄位名稱所在的資料表及欄位相關資訊  

  9. Linux 服务器如何禁止 ping 以及开启 ping

    Linux 默认是允许 ping 响应的,也就是说 ping 是开启的,但 ping 有可能是网络攻击的开始之处,所以关闭 ping 可以提高服务器的安全系数.系统是否允许 ping 由2个因素决定的 ...

  10. 提高 Discuz 门户文章被百度收录的方法

    如果你了解 SEO,你就该清楚使用 canonical URL 标签可以固定网页标准地址,可以提高网页的权重,有利于搜索引擎收录. 例如我的网站拥有两个子域名www.bbseat.com.cn和bbs ...