您的位置:首页 > 运维架构

修改配置文件configparser

2018-01-15 23:05 148 查看
import configparser
start = configparser.ConfigParser()
start.read("config.ini")
section = start.section()
print(section)
options = start.options('mysqld')
print('options')
options1 = start.options('count')
items = start.items('mysqld')
items1 = start.items('count')
print (items)
print (items1)

host = start.get("mysqld","host")
port = start.get("mysqld","port")
user = start.get("mysqld","user")
password = start.get("mysqld","password")

print(host)
print(port)
print(user)
print(password)
'''
start.add_section("add")
添加[add]
start.write(open("config.ini","w"))
写入到配置文件里
'''

if start.has_section("add"):
print("already add")
else:
print("not add ")
start.add_section("add")
start.set("add","name","test")
start.write(open("config.int","w"))

if start.has_section("del"):
print("already del")
else:
print("not del ")
start.remove_section("del")
print(delete success)
start.write(open("config.int","w"))

'''
start.remove_option("count","count_num")
删除[count]的count_num类数据
start.write(open("config.int","w"))
写入到配置文件
'''

if  start.del_option("count","count_num"):
print("if count_num,new delete")
start.remove_option("count","count_num")
start.write(open("config.ini","w"))
print("delete count_num")
else:
print("not count_num")

'''
start.set("count","count_num","22")
start.write(open("config.int","w"))
'''
if start.new_section("count","count_num","2"):
start.set("count","count_num","22")
start.write(open("config.ini","w"))
else:
print("error")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  configparser 运维