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)





你可能感兴趣的:(python)