1. # -*- coding: utf-8 -*-
  2. '''
  3. Version : Python27
  4. Author : Spring God
  5. Date : 2012-4-26
  6. Info : 配置文件ini所在文件夹必需存在,否则抛出IOError异常,自处理之
  7. '''
  8.  
  9. import sys
  10.  
  11. if sys.hexversion >= 0x03000000:
  12. import configparser
  13. else:
  14. import ConfigParser as configparser
  15.  
  16. def set_conf(ini_file, section, key, value):
  17.  
  18. _config = configparser.ConfigParser()
  19.  
  20. try:
  21. _config.read(ini_file)
  22. if not _config.has_section(section):
  23. _config.add_section(str(section))
  24. _config.set(str(section), str(key), str(value))
  25. with open(ini_file, 'w') as _file:
  26. _config.write(_file)
  27. _file.close()
  28. del _file
  29. finally:
  30. del _config
  31.  
  32. def get_conf(ini_file, section, key):
  33.  
  34. _config = configparser.ConfigParser()
  35.  
  36. try:
  37. _config.read(ini_file)
  38. value = _config.get(str(section), str(key))
  39. except configparser.NoSectionError:
  40. value = None
  41. finally:
  42. del _config
  43.  
  44. return value
  45.  
  46. def del_conf(ini_file, section, key = None):
  47.  
  48. _config = configparser.ConfigParser()
  49.  
  50. try:
  51. _config.read(ini_file)
  52. if section != None and _config.has_section(section):
  53. if key != None:
  54. _config.remove_option(section, key)
  55. else:
  56. _config.remove_section(section)
  57. with open(ini_file, 'w') as _file:
  58. _config.write(_file)
  59. _file.close()
  60. del _file
  61. finally:
  62. del _config
  63.  
  64. if __name__ == '__main__':
  65.  
  66. set_conf('set.ini', 'General', 'key', 'value')
  67. #print(get_conf('set.ini', 'General', 'key'))
  68. del_conf('set.ini', 'General', 'key')

python配置文件操作——configparser模块的更多相关文章

  1. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  2. Python 标准库 ConfigParser 模块 的使用

    Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...

  3. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  4. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  5. Python语言的configparser模块便捷的读取配置文件内容

    配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...

  6. python学习之 - configparser模块

    configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...

  7. Python读写操作Excel模块_xlrd_xlwt_xlutils

    Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...

  8. python中的ConfigParser模块

    1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...

  9. python学习 day19 configparser模块 os模块 subprocess模块

    上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...

随机推荐

  1. go 切片

    切片定义 切片是基于数组类型做的一层封装.它非常灵活,可以自动扩容. var a []int //定义一个int类型的空切片 切片初始化, a[start:end]创建一个包括从start到end-1 ...

  2. android开发中常用的快捷键

    Eclipse快捷键-方便查找,呵呵,记性不好 行注释/销注释 Ctrl+/  块注释/销注释/XML注释 Ctrl+Shift+/   Ctrl+Shift+\查找 查找替换 Ctrl+H  Ctr ...

  3. mysql5.7半自动同步设置【转】

    mysql的主从复制主要有3种模式: a..主从同步复制:数据完整性好,但是性能消耗高 b.主从异步复制:性能消耗低,但是容易出现主从数据唯一性问题 c.主从半自动复制:介于上面两种之间.既能很好的保 ...

  4. 缓存数据库-redis数据类型和操作(set)

    一:Redis 集合(Set) Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复 ...

  5. ThinkPHP递归删除栏目

    ThinkPHP递归删除栏目 https://www.cnblogs.com/zlnevsto/p/7051875.html Thinkphp3.2 无限级分类删除,单个删除,批量删除 https:/ ...

  6. 文件下载功能django+js

    1. 功能叙述 前端web页面通过访问url+id的形式访问url lottery/draw/(?P<pk>(\d+))/download/ 后端代码通过orm查询pk相关数据 过滤出自己 ...

  7. CCF CSP 201403-3 命令行选项

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201403-3 命令行选项 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些 ...

  8. HBase(二)CentOS7.5搭建HBase1.2.6HA集群

    一.安装前提 1.HBase 依赖于 HDFS 做底层的数据存储 2.HBase 依赖于 MapReduce 做数据计算 3.HBase 依赖于 ZooKeeper 做服务协调 4.HBase源码是j ...

  9. day5模块学习--random模块

    Python中的random模块用于生成随机数 下面具体介绍random模块的功能:   1.random.random() #用于生成一个0到1的浮点数   随机浮点数:0<= n < ...

  10. customPage.class.php可添加js事件的分页类

    用于ajax动态加载数据的分页类,分页事件可以动态添加,去除了a链接中的href地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...