ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@localhost config]# cat 1c.py import ConfigParser parser=ConfigParser.SafeConfigParser() parser.read(‘/etc/my.cnf‘) print parser.get(‘mysqld‘,‘socket‘) [root@localhost config]# python 1c.py /var/lib/mysql/mysql.sock
1 [root@localhost config]# cat 2c.py 2 import ConfigParser 3 parser=ConfigParser.SafeConfigParser() 4 parser.read(‘/etc/my.cnf‘) 5 print parser.sections() #打印配置文件里面的节点 6 for nodename in parser.sections(): 7 print "nodename:",nodename 8 print "optionsname:",parser.options(nodename) #获取节点名里面的选项 9 for name,value in parser.items(nodename): #以字典的方式返回 10 print "%s=%s"%(name,value) 11 12 [root@localhost config]# python 2c.py 13 [‘mysqld_safe‘, ‘mysqld‘] 14 nodename: mysqld_safe 15 optionsname: [‘log-error‘, ‘pid-file‘] 16 log-error=/var/log/mysqld.log 17 pid-file=/var/run/mysqld/mysqld.pid 18 nodename: mysqld 19 optionsname: [‘datadir‘, ‘socket‘, ‘symbolic-links‘, ‘user‘] 20 datadir=/var/lib/mysql 21 socket=/var/lib/mysql/mysql.sock 22 symbolic-links=0 23 user=mysql
[root@localhost config]# cat 3c.py import ConfigParser parser=ConfigParser.SafeConfigParser(allow_no_value=True) parser.read(‘/etc/my.cnf‘) for node in parser.sections(): for optionname in parser.options(node): print "%s=%s"%(optionname,parser.get(node,optionname)) #利用get方法返回一个值的整型,但是log-bin没有值,所以返回None [root@localhost config]# python 3c.py log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid user=mysql datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-bin=None
1 [root@localhost config]# cat 4c.py 2 import ConfigParser 3 parser=ConfigParser.SafeConfigParser(allow_no_value=True) 4 parser.read(‘/etc/my.cnf‘) 5 for section in [‘mysqld‘,‘mysqld1‘]: 6 print "[%s] is exists?:%s" %(section,parser.has_section(section)) 7 print "[%s]serverid option is exists?:%s"%(section,parser.has_option(section,‘serverid‘)) 8 [root@localhost config]# python 4c.py 9 [mysqld] is exists?:True 10 [mysqld]serverid option is exists?:True 11 [mysqld1] is exists?:False 12 [mysqld1]serverid option is exists?:Fals
1 [root@localhost config]# cat 5c.py 2 import ConfigParser 3 import sys 4 parser=ConfigParser.SafeConfigParser(allow_no_value=True) 5 parser.read(‘/etc/my.cnf‘) 6 parser.add_section(‘mysqld1‘) 7 parser.set(‘mysqld1‘,‘serverid‘,‘2‘) 8 parser.set(‘mysqld1‘,‘log-bin‘) 9 parser.write(sys.stdout) 10 [root@localhost config]# python 5c.py 11 [mysqld1] 12 serverid = 2 13 log-bin 14 15 [mysqld_safe] 16 log-error = /var/log/mysqld.log 17 18 [mysqld] 19 socket = /var/lib/mysql/mysql.sock 20 datadir = /var/lib/mysql 21 log-bin 22 serverid = 2 23 symbolic-links = 0 24 user = mysql
修改serverid属性值并写进my.cnf文件(节点顺序也反了)
1 [root@localhost config]# cat 5c1.py 2 import ConfigParser 3 parser=ConfigParser.SafeConfigParser(allow_no_value=True) 4 parser.read(‘/etc/my.cnf‘) 5 parser.set(‘mysqld‘,‘serverid‘,‘8‘) 6 with open(‘/etc/my.cnf‘,‘w‘) as f: 7 parser.write(f) 8 [root@localhost config]# python 5c1.py 9 [root@localhost config]# cat 5c1.py 10 import ConfigParser 11 parser=ConfigParser.SafeConfigParser(allow_no_value=True) 12 parser.read(‘/etc/my.cnf‘) 13 parser.set(‘mysqld‘,‘serverid‘,‘8‘) 14 with open(‘/etc/my.cnf‘,‘w‘) as f: 15 parser.write(f) 16 [root@localhost config]# cat /etc/my.cnf 17 [mysqld_safe] 18 log-error = /var/log/mysqld.log 19 20 [mysqld] 21 socket = /var/lib/mysql/mysql.sock 22 datadir = /var/lib/mysql 23 log-bin 24 serverid = 8 25 symbolic-links = 0 26 user = mysql
python ConfigParser模块 配置文件解析,布布扣,bubuko.com
原文:http://www.cnblogs.com/pping/p/3893524.html