码迷,mamicode.com
首页 > 其他好文 > 详细

ConfigParser 实例 02

时间:2014-07-22 22:35:12      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:style   文件   io   cti   re   c   

# -*- coding: cp936 -*-

IP1=""  #扫描IP

IP2=""   #当前已经扫到的IP

INITXT="IP.ini"  #INI文件名字

 

import ConfigParser

 

def ini_get():  #读取INI

    try:

        global IP1

        global IP2

        global INITXT

        config = ConfigParser.ConfigParser()

        config.readfp(open(INITXT))

        IP1 = config.get("ipdata","ip1")

        IP2 = config.get("ipdata","ip2")

    except:

        print "读取INI错误"

        ini_add("","")  #写入INI

 

def ini_add(ip1,ip2):  #写入INI

    try:

        global INITXT

        config = ConfigParser.ConfigParser()

        config.add_section("ipdata")# 设置section段及对应的值

        config.set("ipdata","ip1",ip1)

        config.set("ipdata","ip2",ip2)

        config.write(open(INITXT, "w"))# 写入文件

    except:

       print "写入INI错误"

 

def ini_write(ip1,ip2):  #修改INI

    try:

        global INITXT

        config = ConfigParser.ConfigParser()

        config.read(INITXT)

        if not config.has_section("ipdata"):#看是否存在该Section,不存在则创建

            temp = config.add_section("")

        config.set("ipdata","ip1",ip1)

        config.set("ipdata","ip2",ip2)

        config.write(open(INITXT, "r+"))

    except:

        print "修改INI错误"

        ini_add("","")  #写入INI

 

 

if __name__==‘__main__‘:

#    ini_get()  #读取INI

#    print IP1

#    print IP2

 

#    ini_add("222222222","3333333333333")  #写入INI

#    ini_get()  #读取INI

#    print IP1

#    print IP2

 

    ini_write("999999999","0000000000")  #修改INI

    ini_get()  #读取INI

    print IP1

    print IP2

 

import ConfigParser

 

config = ConfigParser.RawConfigParser()

 

config.add_section(‘Section1‘)

config.set(‘Section1‘, ‘an_int‘, ‘15‘)

config.set(‘Section1‘, ‘a_bool‘, ‘true‘)

config.set(‘Section1‘, ‘a_float‘, ‘3.1415‘)

config.set(‘Section1‘, ‘baz‘, ‘fun‘)

config.set(‘Section1‘, ‘bar‘, ‘Python‘)

config.set(‘Section1‘, ‘foo‘, ‘%(bar)s is %(baz)s!‘)

 

# Writing our configuration file to ‘example.cfg‘

with open(‘example.cfg‘, ‘wb‘) as configfile:

    config.write(configfile)

 

#coding:utf-8

 

import ConfigParser

 

class Conf():

    

    def __init__(self,name):

        self.name = name

        self.cp = ConfigParser.ConfigParser()

        self.cp.read(name)

         

            

    def getSections(self):

        return self.cp.sections()

    

    def getOptions(self, section):

        if self.cp.has_section(section):

            return self.cp.options(section)

    

    def getItems(self, section):

        if self.cp.has_section(section):

            return self.cp.items(section)

        

    def getValue(self, section, option):

        if self.cp.has_option(section, option):

            return self.cp.get(section, option)

    

    def setSection(self, section):

        if not self.cp.has_section(section):

            self.cp.add_section(section)

            self.cp.write(open(self.name,‘w‘))

    

    def setValue(self, section, option, value):

        if not self.cp.has_option(section, option):

            self.cp.set(section, option, value)

            self.cp.write(open(self.name,‘w‘))

    

    def delSection(self, section):

        if self.cp.has_section(section):

            self.cp.remove_section(section)

            self.cp.write(open(self.name,‘w‘))

    

    def delOption(self, section, option):

        if self.cp.has_option(section, option):

            self.cp.remove_option(section, option)

            self.cp.write(open(self.name,‘w‘))

            

    def updateValue(self, section, option, value):

        if self.cp.has_option(section, option):

            self.cp.set(section, option, value)

            self.cp.write(open(self.name,‘w‘))

 

if __name__ == "__main__":

    conf = Conf("confx.ini")

    conf.setSection("add")

    conf.setValue("add", "version", "v1.0")

    conf.updateValue("add", "version", "v1.1")

    

    print conf.getItems("add")

    print conf.getSections()

    conf.delSection("add")

 

 

ConfigParser 实例 02,布布扣,bubuko.com

ConfigParser 实例 02

标签:style   文件   io   cti   re   c   

原文地址:http://www.cnblogs.com/mhxy13867806343/p/3860923.html

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