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中的JSON模块
Apr 08 Python
Python计算斗牛游戏概率算法实例分析
Sep 26 Python
python素数筛选法浅析
Mar 19 Python
基于windows下pip安装python模块时报错总结
Jun 12 Python
Python爬虫包BeautifulSoup简介与安装(一)
Jun 17 Python
Python中GeoJson和bokeh-1的使用讲解
Jan 03 Python
python实现五子棋游戏
Jun 18 Python
解决pycharm启动后总是不停的updating indices...indexing的问题
Nov 27 Python
Python内建序列通用操作6种实现方法
Mar 26 Python
Python批量删除mysql中千万级大量数据的脚本分享
Dec 03 Python
基于 Python 实践感知器分类算法
Jan 07 Python
Python绘制词云图之可视化神器pyecharts的方法
Feb 23 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 显示客户端IP与服务器IP的代码
2010/10/12 PHP
php比较多维数组中值的大小排序实现代码
2012/09/08 PHP
Zend Framework教程之Bootstrap类用法概述
2016/03/14 PHP
PHP+Ajax实现无刷新分页实例详解(附demo源码下载)
2016/04/07 PHP
PHP简单日历实现方法
2016/07/20 PHP
FireFox下XML对象转化成字符串的解决方法
2011/12/09 Javascript
js跑马灯代码(自写)
2013/04/17 Javascript
JS 去除Array中的null值示例代码
2013/11/20 Javascript
如何获取网站icon有哪些可行的方法
2014/06/05 Javascript
给应用部分的js代码设定一个统一的入口
2014/06/15 Javascript
轻松实现js图片预览功能
2016/01/18 Javascript
web打印小结
2017/01/11 Javascript
基于 D3.js 绘制动态进度条的实例详解
2018/02/26 Javascript
JavaScript 判断对象中是否有某属性的常用方法
2018/06/14 Javascript
js事件触发操作实例分析
2019/06/21 Javascript
全网小程序接口请求封装实例代码
2020/11/06 Javascript
[36:43]NB vs Optic 2018国际邀请赛小组赛BO1 B组加赛 8.19
2018/08/21 DOTA
Python操作Word批量生成文章的方法
2015/07/28 Python
Python使用Beautiful Soup包编写爬虫时的一些关键点
2016/01/20 Python
Fiddler如何抓取手机APP数据包
2016/01/22 Python
使用Python内置的模块与函数进行不同进制的数的转换
2016/03/12 Python
Python升级导致yum、pip报错的解决方法
2017/09/06 Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
2020/06/22 Python
python如何使用腾讯云发送短信
2020/09/17 Python
Wiggle澳大利亚:自行车、跑步、游泳商店
2020/11/07 全球购物
高中数学教学反思
2014/01/30 职场文书
职业生涯规划书结束语
2014/04/15 职场文书
预备党员公开承诺书
2014/05/28 职场文书
疾病防治方案
2014/05/31 职场文书
如何写早恋检讨书
2014/09/10 职场文书
2015年党风廉政建设工作总结
2015/04/09 职场文书
张思德观后感
2015/06/09 职场文书
导游词之黄帝陵景区
2019/09/16 职场文书
node.js使用express-fileupload中间件实现文件上传
2021/07/16 Javascript
python opencv将多个图放在一个窗口的实例详解
2022/02/28 Python
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏