qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          Python自動(dòng)化測(cè)試 (二) ConfigParser模塊讀寫配置文件

           ConfigParser 是Python自帶的模塊, 用來(lái)讀寫配置文件, 用法及其簡(jiǎn)單。 直接上代碼,不解釋,不多說(shuō)。

            配置文件的格式是: []包含的叫section,    section 下有option=value這樣的鍵值

            配置文件   test.conf

          [section1]
          name = tank
          age = 28

          [section2]
          ip = 192.168.1.1
          port = 8080

            Python代碼

          # -* - coding: UTF-8 -* - 
          import ConfigParser

          conf = ConfigParser.ConfigParser()
          conf.read("c:\\test.conf")

          # 獲取指定的section, 指定的option的值
          name = conf.get("section1", "name")
          print(name)
          age = conf.get("section1", "age")
          print age

          #獲取所有的section
          sections = conf.sections()
          print sections

          #寫配置文件

          # 更新指定section, option的值
          conf.set("section2", "port", "8081")

          # 寫入指定section, 增加新option的值
          conf.set("section2", "IEPort", "80")

          # 添加新的 section
          conf.add_section("new_section")
          conf.set("new_section", "new_option", "http://www.cnblogs.com/tankxiao")

          # 寫回配置文件
          conf.write(open("c:\\test.conf","w"))

          相關(guān)文章

          Python自動(dòng)化測(cè)試 (一) Eclipse+Pydev 搭建開發(fā)環(huán)境

          posted on 2013-07-05 10:43 順其自然EVO 閱讀(294) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2013年7月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 敦化市| 无锡市| 华安县| 绥芬河市| 馆陶县| 博白县| 赤峰市| 彭山县| 紫金县| 阿克苏市| 锡林郭勒盟| 红安县| 安岳县| 修文县| 长垣县| 徐州市| 城口县| 惠安县| 治县。| 兴宁市| 商水县| 柘荣县| 吉木乃县| 柳江县| 崇文区| 洛川县| 民乐县| 洪雅县| 富阳市| 县级市| 裕民县| 上蔡县| 锦屏县| 嘉义市| 龙泉市| 大悟县| 株洲县| 信宜市| 湖北省| 彭州市| 永昌县|