码迷,mamicode.com
首页 > 编程语言 > 详细

python模块ConfigParser操作配置文件

时间:2015-06-04 17:19:55      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:configparser   python   

python模块ConfigParser 操作ini格式文件

cat test.txt

[host]

web01 = 10.10.10.10

web02 = 20.20.20.20


[db]

mysql01 = 1.1.1.1

mysql02 = 2.2.2.2



#!/usr/bin/env python

#coding:utf8

import ConfigParser,string,os,sys

cf = ConfigParser.ConfigParser()

cf.read("test.txt")

s = cf.sections()

print ‘sections:‘,s


o = cf.options(‘db‘)

print ‘options:‘,o

v = cf.items(‘db‘)

print ‘db:‘,v

print ‘-‘ * 60


db_host = cf.get("db","mysql02")

ip = cf.get("host","web02")

print "db_host:",db_host

print "ip:",ip


cf.set("db","mysql01","100.100.100.100")

cf.write(open("test.txt","w"))


cf.add_section("zabbix")

cf.set("zabbix","monitor01","10.17.4.4")

cf.write(open("test.txt","w"))


cf.remove_option("db","mysql02")

cf.remove_section("host")

cf.write(open("test.txt","w"))


本文出自 “赵海华_运维之路” 博客,请务必保留此出处http://baiying.blog.51cto.com/1068039/1658537

python模块ConfigParser操作配置文件

标签:configparser   python   

原文地址:http://baiying.blog.51cto.com/1068039/1658537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!