qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Python自動化測試 (二) ConfigParser模塊讀寫配置文件

           ConfigParser 是Python自帶的模塊, 用來讀寫配置文件, 用法及其簡單。 直接上代碼,不解釋,不多說。

            配置文件的格式是: []包含的叫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"))

          相關文章

          Python自動化測試 (一) Eclipse+Pydev 搭建開發環境

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


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年7月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 正安县| 韩城市| 永修县| 石景山区| 临城县| 禄劝| 濮阳县| 繁峙县| 滨州市| 永仁县| 盖州市| 甘南县| 万源市| 塔河县| 洞口县| 广安市| 镇原县| 浙江省| 固镇县| 泗洪县| 赤峰市| 二连浩特市| 朝阳区| 聂拉木县| 凤庆县| 襄樊市| 闵行区| 黑河市| 宕昌县| 洱源县| 安远县| 博客| 平远县| 阜新市| 德州市| 竹北市| 莱西市| 揭阳市| 盐池县| 阿鲁科尔沁旗| 化德县|