Python configparser模块操作代码实例


Posted in Python onJune 08, 2020

1、生成配置文件

''' 
  生成配置文件
'''
import configparser

config = configparser.ConfigParser()

# 初始化赋值
config["DEFAULT"] = {'ServerAliveInterval': '45',
           'Compression': 'yes',
           'CompressionLevel': '9'}
# 追加
config['DEFAULT']['ForwardX11'] = 'yes'

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'

config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'   # mutates the parser
topsecret['ForwardX11'] = 'no' # same here

with open('example.ini', 'w') as configfile:
  config.write(configfile)

2、读取配置文件

# 读
import configparser
config = configparser.ConfigParser()
config.sections()
config.read('example.ini')
# {'serveraliveinterval': '45', 'compression': 'yes', 'compressionlevel': '9', 'forwardx11': 'yes'}
print(config.defaults())

# hg
print(config['bitbucket.org']["User"])

# 50022
print(config["topsecret.server.com"]["host port"])

3、删除

# 删除(创建一个新文件,并删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections()

config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.cfg","w"))

生成新文件 example.cfg

DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

topsecret.server.com]
host port = 50022
forwardx11 = no

删除,并覆盖原文件

# 删除(删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections()

config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.ini","w"))

4、修改

import configparser

config = configparser.ConfigParser()

config.read('example.ini') #读文件

config.add_section('yuan') #添加section

config.remove_section('bitbucket.org') #删除section
config.remove_option('topsecret.server.com',"forwardx11") #删除一个配置项

config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')
with open('new2.ini','w') as f:
   config.write(f)

生成新文件 new2.ini

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[topsecret.server.com]
host port = 50022
k1 = 11111

[yuan]
k2 = 22222

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python的Twisted框架编写简单的网络客户端
Apr 16 Python
Python中使用ElementTree解析XML示例
Jun 02 Python
python中日志logging模块的性能及多进程详解
Jul 18 Python
解决Python网页爬虫之中文乱码问题
May 11 Python
python检索特定内容的文本文件实例
Jun 05 Python
python简单鼠标自动点击某区域的实例
Jun 25 Python
Win10下Python3.7.3安装教程图解
Jul 08 Python
Python使用贪婪算法解决问题
Oct 22 Python
在Python 的线程中运行协程的方法
Feb 24 Python
python对文件的操作方法汇总
Feb 28 Python
解决python中0x80072ee2错误的方法
Jul 19 Python
python分分钟绘制精美地图海报
Feb 15 Python
pycharm 实现本地写代码,服务器运行的操作
Jun 08 #Python
pycharm软件实现设置自动保存操作
Jun 08 #Python
Python decimal模块使用方法详解
Jun 08 #Python
深入了解python列表(LIST)
Jun 08 #Python
将pycharm配置为matlab或者spyder的用法说明
Jun 08 #Python
基于python实现matlab filter函数过程详解
Jun 08 #Python
浅谈tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意点
Jun 08 #Python
You might like
PHP递归删除目录几个代码实例
2014/04/21 PHP
ThinkPHP整合百度Ueditor图文教程
2014/10/21 PHP
YII2框架中excel表格导出的方法详解
2017/07/21 PHP
ExtJS 简介 让你知道extjs是什么
2008/12/29 Javascript
基于jQuery选择器的整理集合
2013/04/26 Javascript
JS保存、读取、换行、转Json报错处理方法
2013/06/14 Javascript
浅析JavaScript中的delete运算符
2013/11/30 Javascript
JavaScript设计模式之工厂方法模式介绍
2014/12/28 Javascript
JavaScript中关联原型链属性特性
2016/02/13 Javascript
Jquery EasyUI实现treegrid上显示checkbox并取选定值的方法
2016/04/29 Javascript
jQuery实现的自定义滚动条实例详解
2016/09/20 Javascript
基于JS实现checkbox全选功能实例代码
2016/10/31 Javascript
js实现小时钟效果
2020/03/25 Javascript
[58:58]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第二场
2018/04/05 DOTA
python 线程的暂停, 恢复, 退出详解及实例
2016/12/06 Python
fastcgi文件读取漏洞之python扫描脚本
2017/04/23 Python
Python实现模拟分割大文件及多线程处理的方法
2017/10/10 Python
python使用锁访问共享变量实例解析
2018/02/08 Python
python next()和iter()函数原理解析
2020/02/07 Python
Python2 与Python3的版本区别实例分析
2020/03/30 Python
Python退出时强制运行一段代码的实现方法
2020/04/29 Python
Python如何读取、写入JSON数据
2020/07/28 Python
分享一个python的aes加密代码
2020/12/22 Python
HTML5新增form控件和表单属性实例代码详解
2019/05/15 HTML / CSS
香港太阳眼镜网上商店:SmartBuyGlasses香港
2016/07/22 全球购物
Columbia美国官网:美国著名的户外服装品牌
2016/11/24 全球购物
帕克纽约:PARKER NY
2018/12/09 全球购物
UNOde50美国官网:西班牙珠宝品牌
2020/08/15 全球购物
介绍一下mysql的日期和时间函数
2013/03/28 面试题
工程专业应届生求职信
2014/02/19 职场文书
课例研修方案
2014/05/31 职场文书
农村文化建设标语
2014/10/07 职场文书
西双版纳导游词
2015/02/03 职场文书
地雷战观后感
2015/06/09 职场文书
推普标语口号大全
2015/12/26 职场文书
2019年鼓励无偿献血倡议书
2019/09/17 职场文书