configparser模块

发布时间:2020-07-11 10:22:48 作者:坚持和学习
来源:网络 阅读:260

生成文件

import configparser

config = configparser.ConfigParser()
#在DEFAULT节点下添加,类似字典的key和value
config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'}

config['bitbucket.org'] = {"alex":33}
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

config['DEFAULT']['ForwardX11'] = 'yes' #在在DEFAULT节点下添加key为'ForwardX11,value为yes的数据
with open('example.ini', 'w') as configfile:
config.write(configfile)

[DEFAULT] 创建文件的内容

#serveraliveinterval = 45
#compression = yes
#compressionlevel = 9
#forwardx11 = yes

#[bitbucket.org]
#alex = 33
#user = hg

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

configparser文件读取写入

import configparser

conf=configparser.ConfigParser()
conf.read("example.ini")
print(conf.sections())#打印节点(除了“DEFAULT”以外的节点)
print(conf["bitbucket.org"]["user"])
print(conf.defaults())#打印[DEFAULT]

#删除后,创建新文件写入
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.cfg', "w"))

#删除后,覆盖原文件
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.ini', "w"))

推荐阅读:
  1. Python configparser模块 与 subprocess 模块
  2. 【Python模块】configparser模块

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

configparser 模块 igp

上一篇:html5需要遵守的原则有哪些

下一篇:linux下PHP安装openssl扩展

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》