栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python配置文件练习操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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文件处理--文件练习

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/226071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号