ConfigParser 執行個體 02

來源:互聯網
上載者:User

標籤: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")

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.