python读写配置文件还是比较方便得。
1) 基本的读取配置文件
-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
2) 基本的写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
配置文件如下:
点击(此处)折叠或打开
1. [db]
2. db_port = 3306
3. db_user = root
4. db_host = 127.0.0.1
5. db_pass = xgmtest
6.
7. [concurrent]
8. processor = 20
9. thread = 10
示例代码如下
点击(此处)折叠或打开
1. #/usr/bin/python
2.
3. import ConfigParser
4. import string, os, sys
5.
6. cf = ConfigParser.ConfigParser()
7.
8. cf.read("test.conf")
9.
10. #return all section
11. secs = cf.sections()
12. print 'sections:', secs
13.
14. opts = cf.options("db")
15. print 'options:', opts
16.
17. kvs = cf.items("db")
18. print 'db:', kvs
19.
20. #read by type
21. db_host = cf.get("db", "db_host")
22. db_port = cf.getint("db", "db_port")
23. db_user = cf.get("db", "db_user")
24. db_pass = cf.get("db", "db_pass")
25.
26. #read int
27. threads = cf.getint("concurrent", "thread")
28. processors = cf.getint("concurrent", "processor")
29.
30. print "db_host:", db_host
31. print "db_port:", db_port
32. print "db_user:", db_user
33. print "db_pass:", db_pass
34.
35. print "thread:", threads
36. print "processor:", processors
37.
38.
39. #modify one value and write to file
40. cf.set("db", "db_pass", "xgmtest")
41. cf.write(open("test.conf", "w"))
3) Python的ConfigParser Module 中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。 RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的 解析。
设定配置文件test2.conf
点击(此处)折叠或打开
1. [portal]
2. url = http://%(host)s:%(port)s/Portal
3. host = localhost
4. port = 8080
使用RawConfigParser:
点击(此处)折叠或打开
1. import ConfigParser
2.
3. cf = ConfigParser.RawConfigParser()
4.
5. print "use RawConfigParser() read"
6. cf.read("test2.conf")
7. print cf.get("portal", "url")
8.
9. print "use RawConfigParser() write"
10. cf.set("portal", "url2", "%(host)s:%(port)s")
11. print cf.get("portal", "url2")
得到终端输出:
点击(此处)折叠或打开
1. use RawConfigParser() read
2. http://%(host)s:%(port)s/Portal
3. use RawConfigParser() write
4. %(host)s:%(port)s
改用ConfigParser:
点击(此处)折叠或打开
1. import ConfigParser
2.
3. cf = ConfigParser.ConfigParser()
4.
5. print "use ConfigParser() read"
6. cf.read("test2.conf")
7. print cf.get("portal", "url")
8.
9. print "use ConfigParser() write"
10. cf.set("portal", "url2", "%(host)s:%(port)s")
11. print cf.get("portal", "url2")
得到终端输出:
点击(此处)折叠或打开
1. use ConfigParser() read
2. http://localhost:8080/Portal
3. use ConfigParser() write
4. localhost:8080
改用SafeConfigParser:
点击(此处)折叠或打开
1. import ConfigParser
2.
3. cf = ConfigParser.SafeConfigParser()
4.
5. print "use SafeConfigParser() read"
6. cf.read("test2.conf")
7. print cf.get("portal", "url")
8.
9. print "use SateConfigParser() write"
10. cf.set("portal", "url2", "%(host)s:%(port)s")
11. print cf.get("portal", "url2")
得到终端输出(效果同ConfigParser):
点击(此处)折叠或打开
1. use SafeConfigParser() read
2. http://localhost:8080/Portal
3. use SateConfigParser() write
4. localhost:8080