python读写ini配置文件
像邮箱等信息是可以写在配置文件里面的,python有一个配置模块ConfigParser,可以处理配置文件信息
目录
1、配置模块ConfigParser
2、基本应用
1、配置模块ConfigParser
关于ini文件:
ini文件中,每一部分以[section]开始 option=value结尾;备注以;开头;section不可重名
如:
用ConfigParser模块中的ConfigParser类读取ini文件,然后使用ConfigParser类中的get方法,然后读取到value值
conf=ConfigParser.ConfigParser()
key1=conf.get("section","option") # 读取到第一个section的中的option值
key2=conf.get("section2","option")# 读取到第二个section的中的option值
2、基本应用
第一步:在pycharm中创建一个.ini文件
new->file ,命名为new.ini,写入信息如下:
[email_qq] sender=123@qq.com
psw=123456
port=465
smtp_server=smtp.qq.com
receiver=456@qq.com
第二步:在pycharm中创建config.py文件,调用ConfigParser模块中方法读取
# coding:utf-8 import ConfigParser
import os
#用os模块来读取
curpath=os.path.dirname(os.path.realpath(__file__))
cfgpath=os.path.join(curpath,"peizhi.ini") #读取到本机的配置文件 #调用读取配置模块中的类
conf=ConfigParser.ConfigParser() #调用get方法,然后获取配置的数据
sender=conf.get("email_qq","sender")
psw=conf.get("email_qq","psw")
stmp=conf.get("email_qq","stmp")
port=conf.get("email_qq","port")
第三步:在pycharm中run_allcase.py文件的main函数中,调用配置读取配置文件
#调用配置文件的模块
from config import config #调用这个模块中的参数
sender=config.sender
psw=config.psw
receiver=config.receiver
port=config.port #再调用run_allcase中发送邮件的方法,将值填进去
send_mail(sender,psw,receiver,port)
python读写ini配置文件的更多相关文章
- python 读写INI配置文件
# -*- coding: utf-8 -*-import ConfigParserimport os '''读写配置文件的类[section]logpath = D:\log\imageminsiz ...
- C# 读写 ini 配置文件
虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...
- [转]VB 读写ini 配置文件
转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...
- 【python-ini】python读写ini文件
[python-ini]python读写ini文件 本文实例讲述了Python读写ini文件的方法.分享给大家供大家参考.具体如下: 比如有一个文件update.ini,里面有这些内容: 1 2 ...
- 自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
- 引用“kernel32”读写ini配置文件
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
C# 文件的一些基本操作 2009-07-19 来自:博客园 字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...
- python读取ini配置文件的示例代码(仅供参考)
这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...
- Python读取ini配置文件(接口自动测试必备)
前言 大家应该接触过.ini格式的配置文件.配置文件就是把一些配置相关信息提取出去来进行单独管理,如果以后有变动只需改配置文件,无需修改代码. 特别是后续做自动化的测试,代码和数据分享,进行管理.比如 ...
随机推荐
- Macrotask Queue和Microtask Quque
from:http://www.jianshu.com/p/3ed992529cfc setImmediate(function(){ console.log(1); },0); setTimeout ...
- PAT Advanced 1036 Boys vs Girls (25 分)
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- Linux之文件属性、权限
Linux中的3种身份:1. owner(文件所有者) 2. group(用户组) 3. others(其他) Linux中的3中权限:1. r(可读) 2. w(可写) 3. x(可执行) * 所有 ...
- Xor Sum 2 AtCoder - 4142 (异或前缀和性质+ 双指针)
Problem Statement There is an integer sequence A of length N. Find the number of the pairs of intege ...
- libevent cs
int evutil_make_listen_socket_reuseable(evutil_socket_t sock): 相当于执行以下操作 int one = 1; setsockopt(soc ...
- airflow 简介
转载:https://zhuanlan.zhihu.com/p/36043468 简介 Apache-Airflow 是Airbnb开源的一款数据流程工具,目前是Apache孵化项目.以非常灵活的方式 ...
- git上拉项目
- Python(1) 整型与浮动型
整型与浮动型 整数/浮动数=浮点型整数/整数 = 浮点型 例如:>>> type(1/1)<class 'float'>>>> type(1/1.0)& ...
- iterm2 多频操作
新开多个table窗口 右键 move session to split pane 选择窗口 command + 窗口号 灰色的窗口右键 toggle Broadcasting input
- 并行操作多个序列map
>>> def add1(a): return a + 1 >>> def add2(a,b): return a + b >>> def add ...