【python】configparser读写配置文件

python版本3.6

参考文章:https://docs.python.org/3.6/library/configparser.html

用法:

建立一个test.conf文件,用于读写配置

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no


读写方法

# -* - coding: UTF-8 -* -
import configparser

conf = configparser.ConfigParser()
conf.read("./test.conf")

# 获取指定的section, 指定的option的值
name = conf.get("DEFAULT", "Forwardx11")
print(name)

# 更新指定section, option的值
conf.set("topsecret.server.com", "port", "8081")

# 写回配置文件
conf.write(open("./test.conf","w"))


更多用法参考文章 https://docs.python.org/3.6/library/configparser.html



知行办公,专业移动办公平台 https://zx.naton.cn/

原创团队
【总监】十二春秋之,[email protected]
【Master】zelo,[email protected];【运营】运维 艄公 [email protected]
【产品设计】流浪猫,[email protected];【体验设计】兜兜,[email protected]
【iOS】淘码小工,[email protected];iMcG33K,[email protected]
【Android】人猿居士,[email protected] 思路的顿悟,[email protected]
【java】首席工程师MR_W,[email protected];【测试】 土镜问道,[email protected]
【数据】fox009521,[email protected];【安全】保密,你懂的。








你可能感兴趣的:(python)