python配置文件操作——configparser模块
# -*- coding: utf-8 -*-
'''
Version : Python27
Author : Spring God
Date : 2012-4-26
Info : 配置文件ini所在文件夹必需存在,否则抛出IOError异常,自处理之
''' import sys if sys.hexversion >= 0x03000000:
import configparser
else:
import ConfigParser as configparser def set_conf(ini_file, section, key, value): _config = configparser.ConfigParser() try:
_config.read(ini_file)
if not _config.has_section(section):
_config.add_section(str(section))
_config.set(str(section), str(key), str(value))
with open(ini_file, 'w') as _file:
_config.write(_file)
_file.close()
del _file
finally:
del _config def get_conf(ini_file, section, key): _config = configparser.ConfigParser() try:
_config.read(ini_file)
value = _config.get(str(section), str(key))
except configparser.NoSectionError:
value = None
finally:
del _config return value def del_conf(ini_file, section, key = None): _config = configparser.ConfigParser() try:
_config.read(ini_file)
if section != None and _config.has_section(section):
if key != None:
_config.remove_option(section, key)
else:
_config.remove_section(section)
with open(ini_file, 'w') as _file:
_config.write(_file)
_file.close()
del _file
finally:
del _config if __name__ == '__main__': set_conf('set.ini', 'General', 'key', 'value')
#print(get_conf('set.ini', 'General', 'key'))
del_conf('set.ini', 'General', 'key')
python配置文件操作——configparser模块的更多相关文章
- python:利用configparser模块读写配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- Python语言的configparser模块便捷的读取配置文件内容
配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...
- python学习之 - configparser模块
configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...
- Python读写操作Excel模块_xlrd_xlwt_xlutils
Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...
- python中的ConfigParser模块
1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...
- python学习 day19 configparser模块 os模块 subprocess模块
上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...
随机推荐
- 22 Gobs of data 设计和使用采集数据的包
Gobs of data 24 March 2011 Introduction To transmit a data structure across a network or to store it ...
- Windows 10安装uWSGI:不可行、失败了
Windows 10家庭中文版,Python 3.6.4,uwsgi-2.0.17.tar.gz,压缩工具-7-zip 提示:请不要和我一样尝试,浪费时间,去Linux上玩吧! 几个小时的安装经历 昨 ...
- Java代码优化总结
代码优化是一个很重要的课题.一般来说,代码优化的目标主要有两个,一个是减小代码的体积,另一个是提高代码运行的效率. 代码优化的细节有很多,此处列举部分: 1.尽量指定类.方法的final修饰符. 带有 ...
- jQuery之字体大小的设置
先获取字体大小,进行处理. 再将修改的值保存. slice() 方法可从已有的数组中返回选定的元素.arrayObject.slice(start,end).start 必需.规定从何处开始选 ...
- javaweb笔记二
web服务器:实现服务器的开启,监听端口,接收客户端请求,产生响应.响应信息只能是静态的HTML,缺乏灵活性.web容器:是辅助应用的一种方式,是为了解决web服务器缺陷而产生的.可以将请求信息处理完 ...
- Git missing in VS Code – No source control providers
解决办法:管理->设置->搜索[git.enabled]和[git.path],分别设置下即可. 注意"git.enabled: true",只设置git.path是不 ...
- jQuery使用JSONP时的错误处理
概述 什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很 ...
- HTML标签列表总览
超文本标记语言(简称:HTML)标记标签通常被称为HTML标签,HTML标签是HTML语言中最基本的单位,HTML标签是HTML(标准通用标记语言下的一个应用)最重要的组成部分.HTML标签的大小写无 ...
- 使用Kafka、Elasticsearch、Grafana搭建业务监控系统(三)Elasticsearch
https://blog.csdn.net/tonywu1992/article/details/83576863
- jquery跨域请求事例
//js发送跨域请求部分var requesturl = 'url'; $.ajax({ type:'GET', url:requesturl, data:{'qNum':num}, dataType ...