用python读取配置文件config.ini
还在学习中。。。写的有点凌乱
感觉还是应该先学会读取配置文件才行,把一些经常需要修改的但是又经常需要用到的参数放到配置文件中方便使用(我是这么觉得的)
首先是config.ini的存放位置,我们把它放在根目录下(当然也可以随便想放哪放哪)
proDir = os.path.split(os.path.realpath(__file__))[0] //根目录地址
configPath = os.path.join(proDir, "config.ini") //存放在根目录下,文件名是config.ini
config.ini的内容如下:
[EMAIL]
mail_host = smtp.163.com
mail_user = abc@163.com
mail_pass = 123456
mail_port = 25
sender = abc@163.com
receiver = 123456@qq.com/1254367@qq.com
subject = Interface Test Report
content = "All interface test has been complited\nplease read the report file about the detile of result in the attachment."
testuser = Someone
on_off = off [HTTP]
scheme = http
baseurl = www.baidu.com
port = 8080
timeout = 10.0 [HEADERS]
siteuid = all
clientid = 100
token_v = 213612368807
token_u = 320012369021 [DATABASE]
host = localhost
username = root
password = root
port = 3306
database = test
既然配置文件有了,我们就来读取配置文件吧
import os
import codecs
import configparser proDir = os.path.split(os.path.realpath(__file__))[0]
configPath = os.path.join(proDir, "config.ini") class ReadConfig:
def __init__(self):
fd = open(configPath)
data = fd.read() # remove BOM
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
file = codecs.open(configPath, "w")
file.write(data)
file.close()
fd.close() self.cf = configparser.ConfigParser()
self.cf.read(configPath) def get_email(self, name):
value = self.cf.get("EMAIL", name)
return value def get_http(self, name):
value = self.cf.get("HTTP", name)
return value def get_headers(self, name):
value = self.cf.get("HEADERS", name)
return value def set_headers(self, name, value):
self.cf.set("HEADERS", name, value)
with open(configPath, 'w+') as f:
self.cf.write(f) def get_url(self, name):
value = self.cf.get("URL", name)
return value def get_db(self, name):
value = self.cf.get("DATABASE", name)
return value
用python读取配置文件config.ini的更多相关文章
- python读取配置文件(ini、yaml、xml)
python读取配置文件(ini.yaml.xml)
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- python读取配置文件的方式
python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...
- python 读取配置文件总是报错 configparser.NoSectionError: No section:
本文为作者原创,禁止转载,违者必究法律责任!!! python 读取配置文件总是报错 configparser.NoSectionError: No section: 无论是 python2的版本,还 ...
- 使用python读取配置文件并从mysql数据库中获取数据进行传参(基于Httprunner)
最近在使用httprunner进行接口测试,在传参时,用到了三种方法:(1)从csv文件中获取:(2)在config中声名然后进行引用:(3)从函数中获取.在测试过程中,往往有些参数是需要从数据库中获 ...
- MySQL Cluster 配置文件(config.ini)详解
MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...
- python 读取配置文件ini ---ConfigParser
Python读取ini文件需要用到 ConfigParser 模块 关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/c ...
- python读取配置文件&&简单封装
之前有做过把爬虫数据写到数据库中的练习,这次想把数据库信息抽离到一个ini配置文件中,这样做的好处在于可以在配置文件中添加多个数据库,方便切换(另外配置文件也可以添加诸如邮箱.url等信息) 1.co ...
- Python+Selenium中级篇之-Python读取配置文件内容
本文来介绍下Python中如何读取配置文件.任何一个项目,都涉及到了配置文件和管理和读写,Python支持很多配置文件的读写,这里我们就介绍一种配置文件格式的读取数据,叫ini文件.Python中有一 ...
随机推荐
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- ssh启动失败
调试了两个小时.ssh启动不了. service ssh start /etc/init.d/ssh start 都尝试了,还是没法启动. [ 是否启动,可以命令行: ps -s | grep ssh ...
- SpringBoot 修改仓库
SpringBoot使用aliyun仓库 maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
- 【洛谷P2279】[HNOI2003]消防局的设立
消防局的设立 题目链接 贪心:每次取出深度最大的节点,若没有被覆盖到,要想覆盖它, 最优的做法显然是将它的爷爷设为消防局 (因为该节点深度为最大,选兄弟.父亲所覆盖的节点,选了爷爷后都能够覆盖) 用优 ...
- 在js中获取request域中的内容
1.可以使用小脚本<%%>实现: var pro_id=<%request.getPro_id()%>; 2.使用隐藏域实现: <input type="hid ...
- MySql第几行到第几行语句
1.查询第一行记录: select * from table limit 1 2.查询第n行到第m行记录 select * from table1 limit n-1,m-n; SELECT * FR ...
- Oracle 左连接(+)加号用法及常用语法之间的关系
本文目的: 通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法 分析步骤: 1.首先创建测试表的结构: create table test_left_a (a ...
- Swift_属性
Swift_属性 点击查看源码 class DataImporter { var fileName = "data.txt" init() { print("初始化&qu ...
- DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载 吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...
- 【CodeForces 129 B】Students and Shoelaces(拓扑排序)
Anna and Maria are in charge of the math club for junior students. When the club gathers together, t ...