configparser模块:用于按一定格式创建配置文件

  创建

import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'default': 'yes'}
config['path'] = {'userinfo': r'E:\pycharm\学习\day29\userinfo'}
with open('userinfo.ini', 'w', encoding='utf-8') as f:
config.write(f)

  完成后的文件:

[DEFAULT]
default = yes [path]
userinfo = E:\pycharm\学习\day29\userinfo

  查看

print(config.sections())  # []        因为还没有读取文件
config.read('userinfo.ini', encoding='utf-8')
print(config.sections()) # ['path'] 读出节点 ['DEFAULT']为默认节点 不打印
print('path' in config) # True 判断某节点是否在配置文件中
print(config['path']['userinfo']) # E:\pycharm\学习\day29\userinfo 读取节点下的配置项 没有该项目标报错
print(config.get('path', 'userinfo')) # E:\pycharm\学习\day29\userinfo for k in config['path']: # 打印'path'节点下的配置项的同时还会打印默认节点下的所有项
print(k) # userinfo default print(config.items('path')) # [('default', 'yes'), ('userinfo', 'E:\\pycharm\\学习\\day29\\userinfo')]

  增加

config.read('userinfo.ini', encoding='utf-8')
config.add_section('IP') # 增加节点
print(config.sections()) # ['path', 'IP']
config.set('IP', 'ip', '192.168.1.1') # 给节点增加配置项
config.set('path', 'userinfo', 'None') # 修改配置项
print(config['IP']['ip']) # 192.168.1.1
print(config['path']['userinfo']) # None
config.write(open('userinfo.ini', 'w', encoding='utf-8')) # 将修改重新写回文件

  删除

config.read('userinfo.ini', encoding='utf-8')
print(config.sections()) # ['path', 'IP']
config.remove_section('IP') # 删除节点
print(config.sections()) # ['path']
print(config.items('path')) # [('default', 'yes'), ('userinfo', 'None')]
config.remove_option('path', 'userinfo') # 删除节点中的配置项
print(config.items('path')) # [('default', 'yes')]
config.write(open('userinfo.ini', 'w', encoding='utf-8')) # 将修改重新写回文件

python模块之configparser模块的更多相关文章

  1. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

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

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

  3. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

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

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

  5. python基础14 ---函数模块4(configparser模块)

    configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...

  6. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

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

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

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

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

  9. 《Python》hashlib模块、configparser模块、logging模块

    一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...

  10. Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块

    一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.co ...

随机推荐

  1. SDUT-3378_数据结构实验之查找六:顺序查找

    数据结构实验之查找六:顺序查找 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在一个给定的无序序列里,查找与给定关键字 ...

  2. phpcms 允许英文目录有空格

    大家都用过phpcm添加栏目吧,在添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文 ...

  3. Intersection of Two Linked Lists两链表找重合节点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. Permutations 全排列 回溯

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  5. JAVA-WEB-错误之-'OPTION SQL_SELECT_LIMIT=DEFAULT'

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...

  6. oracle审计的格式

    audit table; audit table by xxx(username); audit table by xxx(username) whenever not successful; 系统表 ...

  7. C++ lambda表达式总结

    一个lambda表达式用于创建闭包.lambda表达式与任何函数类似,具有返回类型.参数列表和函数体.与函数不同的是,lambda能定义在函数内部.lambda表达式具有如下形式 [ capture ...

  8. 如何在云上使用confd+ACM管理敏感数据

    在前面的一些文章中,我们介绍了如何在云上安全的存放配置数据,但是上面的方法都是有代码侵入性的,也就是说需要修改应用程序,本文会讲解如何使用 confd+ACM 在不修改代码的情况下动态修改应用所需的配 ...

  9. thinkphp5.0中英文切换

    首先来看下它的配置: // 是否开启多语言 'lang_switch_on' => true, //语音列表 'lang_list' => ['zh-cn','en-us'], // 获取 ...

  10. Java练习 SDUT-1117_求绝对值(选择结构)

    C语言实验--求绝对值(选择结构) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 从键盘上输入任意一个整数,然后输出它 ...