【Python 3.6.1】写配置文件(二)
一、创建配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@dt xxf]# cat cc.py import configparser cf = configparser.ConfigParser() cf['default']={'host':'192.168.1.5', 'port':'1521', 'service_name':'ORCL'} cf['136']={} cf['136']['host']='192.168.1.136' cf['138']={} ad=cf['138'] ad['host']='192.168.1.138' ad['port']='1526' ad['service_name']='ORA11GR2' cf['139']={'host':'192.168.1.139','port':'1521','service_name':'ORCL'} cf['default']['dt'] = 'yes' with open('example.ini','w') as xxf: cf.write(xxf) [root@dt xxf]# |
二、运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[root@dt xxf]# ls cc.py db.conf fun.py hello.py rconf.py wconf.py [root@dt xxf]# [root@dt xxf]# python3 cc.py [root@dt xxf]# [root@dt xxf]# ls cc.py db.conf example.ini fun.py hello.py rconf.py wconf.py [root@dt xxf]# [root@dt xxf]# cat example.ini [default] host = 192.168.1.5 port = 1521 service_name = ORCL dt = yes [136] host = 192.168.1.136 [138] host = 192.168.1.138 port = 1526 service_name = ORA11GR2 [139] host = 192.168.1.139 port = 1521 service_name = ORCL [root@dt xxf]# |