摘要:本文主要向大家介绍了Python语言配置文件练习操作,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言配置文件练习操作,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
源码
#!/usr/bin/env python
# coding=utf-8
#import os
import ConfigParser
'''
1: dump ini
2: del section
3: del item
4: modify item
5: add section
6: save modify
'''
class student_info(object):
def __init__(self, recordfile):
self.logfile = recordfile
self.cfg = ConfigParser.ConfigParser()
def cfg_load(self):
self.cfg.read(self.logfile)
def cfg_dump(self):
se_list = self.cfg.sections()
print("====================>")
for se in se_list:
print se
print self.cfg.items(se)
print("<====================")
def delete_item(self, section, key):
self.cfg.remove_option(section, key)
def delete_section(self, section):
self.cfg.remove_section(section)
def add_setction(self, section):
self.cfg.add_section(section)
def set_item(self, section, key, value):
self.cfg.set(section, key, value)
def save(self):
fp = open(self.logfile, 'w')
self.cfg.write(fp)
fp.close()
if __name__ == '__main__':
info = student_info('./imooc.txt')
info.cfg_load()
info.cfg_dump()
info.set_item('userinfo', 'pwd', 'abc')
info.cfg_dump()
info.add_setction('login')
info.set_item('login', '2016-1008', '20')
info.cfg_dump()
info.save()
imooc.txt
[userinfo]
name = johntian
pwd = abc
[study]
python_base = 15
python_junior = 20
linux_base_ = 15
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号