1、基本介绍

一般情况下为了方便代码维护,我们把经常需要修改的参数放到配置文件中,可以在项目中新建一个config文件夹,存放config.ini配置文件。

2、具体配置格式

config.ini配置如下:

#config.ini文件

# this is config file, only store browser type and server URL

[browserType]

#browserName = Firefox

browserName = Chrome

#browserName = IE

[testServer]

#URL = https://www.tes1.com

#URL = http://www.test2.com

#URL = https://www.test3com

URL = http://www.baidu.com

3、配置文件的引用

import configparser

import os.path

# Open a configuration file

config = configparser.ConfigParser()

file_path = os.path.dirname(os.path.abspath('.')) + '\config\config.ini' #返回上级目录再访问某文件(路径根据实际情况自定义)

config.read(file_path)

browser = config.get("browserType", "browserName")

url = config.get("testServer", "URL")

print(browser)

print(url)

# for section in config.sections():

# print ("In section %s" % section)

# for (key,value) in config.items(section):

# print (" Key %s has value %s" % (key,value))

代码执行结果如下: