Python+selenium之讀取配置文件內容
- Python支持很多配置文件的讀寫,此例子中介紹一種配置文件的讀取數據,叫ini文件,python中有一個類ConfigParser支持讀ini文件。
- 將ini文件命名為config.ini,代碼如下所示
#this is config file,only store browser type and server URL
[browserType]
#browserName=Firefox
browserName=Chrome
#browserName=IE
[testServer]
3.然后新建一個測試python文件,命名為testconfig.py,測試腳本如下所示
#coding:utf-8
import ConfigParser
import os
class TestReadConfig(object):
def get_value(self):
root_dir=os.path.dirname(os.path.abspath('.'))#表示獲取當前文件夾的所在的目錄
print root_dir
config=ConfigParser.ConfigParser()
file_path=os.path.dirname(os.path.abspath('.'))+'/pro1/config/config.ini'
config.read(file_path)
browser=config.get("browserType","browserName")
url=config.get("testServer","URL")
return (browser,url)
trcf=TestReadConfig()
print trcf.get_value()
注意:config.read(file_path),file_path這里的路徑需寫對,若不太清楚路徑,可以通過pycharm中的Debug來設置斷點,進行查看。【F8】運行到下一行
4.測試結果如下圖所示:

詳情參考:http://blog.csdn.net/u011541946/article/details/70174276
