python ConfigParser 模块学习

因为要用到配置文件,所以自己查了下python有这么个模块ConfigParser.然后就学了下,超级的简单

[mysqld]
datadir=/search/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1


# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

主要由以下几个API

1.获取一个ConfigParser对象

conf=ConfigParser.ConfigParser()

2.获取配置文件的内容

conf.read(“test.cfg”)

3.获取所有的section,list类型

secs=conf.sections()

print secs

4.获取某个section下面的option ,list类型

options=conf.options(sec)

5.获取某个option的value

value=conf.get(section,option)

6.设置某个option的value

conf.set(section,option,value)

但是需要写回cf.write(open("test.conf", "w"))

7.添加section

conf.add_section(section)

conf.write(fp)

8.删除section

conf.remove_section(section)

conf.write(fp)

9.删除option

conf.remove_option(section,option)

conf.write(fp)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值