码迷,mamicode.com
首页 > 编程语言 > 详细

python + selenium -- 读取配置文件内容

时间:2017-07-19 14:15:24      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:firefox   使用   parse   .config   os.path   sof   记录   os模块   elf   

        任何一个项目,都涉及到了配置文件和管理和读写,python 支持很多配置文件的读写本文记录使用 python + selenium自动化过程中,学习的使用python自带的ConfigParser类读取ini配置文件方法。

 

1、在所在项目新建一个文件夹,如config,在配置文件中新建一个文件,如config.ini

配置文件填写内容如下:

 

1 [broswer_name]
2 broswer = firefox
3 
4 [server]
5 server = http://www.baidu.com/

 

2、使用系统自带的os模块获取文件路径

百度搜了很多的方式来获取文件绝对路径,如下方式最佳

1 os.path.abspath(os.path.join(config,config.ini))

 

3、编写读取配置文件的类,方便后续调用

# coding=utf-8
import ConfigParser
import os

class Config_read(object):
    def get_value(self):
        #file_path = os.path.dirname(os.path.realpath(__file__)) + os.path.join(r‘\config‘,‘config.ini‘)
        file_path = os.path.abspath(os.path.join(config,config.ini))

        config = ConfigParser.ConfigParser()
        config.read(file_path)
        #print file_path

        browser = config.get("broswer_name", "broswer") #分别代表所在区域名 和变量名
        url = config.get("server", "server")
        return (browser, url)

if __name__ == __main__:
    trcf = Config_read()
    print trcf.get_value()

 

 

 

python + selenium -- 读取配置文件内容

标签:firefox   使用   parse   .config   os.path   sof   记录   os模块   elf   

原文地址:http://www.cnblogs.com/Detector/p/7204707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!