標籤:live 添加 ring user options 操作 string 多個參數 write
configparser模組
一、configparser模組
1、什麼是configparser模組:configparser模組操作設定檔,設定檔的格式與windows ini和linux的cf檔案類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值),其設定檔(INI檔案)由節(section)、鍵、值組成。
2、configparser模組簡介。
ConfigParser 是用來讀取設定檔的包。設定檔的格式如下:中括弧“[ ]”內包含的為section。section 下面為類似於key-value 的配置內容。
3、如何產生一個configparser設定檔。
1.首先import ConfigParser來調用ConfigParser模組 #import configparser
2.建立一個ConfigParser設定檔對象。#config = configparser.ConfigParser()
3.往設定檔中新增內容。#config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘, ‘Compression‘: ‘yes‘ } ; config[‘bitbucket.org‘] = {‘User‘:‘hg‘}
4.開啟或建立一個設定檔,然後將設定檔對象內的東西添加到設定檔中。
with open(‘example.ini‘, ‘w‘) as configfile:
config.write(configfile)
5.讀取該設定檔。#config.read(‘設定檔名稱‘)
6.使用configparser模組解析設定檔時,發現的問題:
參數名稱的大寫全部會轉換為小寫。
參數名稱不能含有[,]
如果含有多個名字相同的section時,會以最後一個section為準
7.讀取設定檔和寫入設定檔。
-read(filename) 直接讀取ini檔案內容
-sections() 得到所有的section,並以列表的形式返回
-options(section) 得到該section的所有option
-items(section) 得到該section的所有索引值對
-get(section,option) 得到section中option的值,返回為string類型
-getint(section,option) 得到section中option的值,返回為int類型
-add_section(section) 添加一個新的section
-set( section, option, value) 對section中的option進行設定
8.案例測試
詳見:http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944
二、subprocess模組
1、
python基礎13 ---函數模組4(configparser模組)