python-day27–configparser模块

python-day27–configparser模块 标签:live default code

python-day27–configparser模块

标签:livedefaultcode操作serverblogpythoconfclosed

1.来看一个好多软件的常见文档格式如下:

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

[bitbucket.org]
User = hg

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

View Code

如果想用python生成一个这样的文档怎么做呢?

import configparser

config = configparser.ConfigParser()

config[\”DEFAULT\”] = {‘ServerAliveInterval‘: ‘45‘,
‘Compression‘: ‘yes‘,
‘CompressionLevel‘: ‘9‘,
‘ForwardX11‘:‘yes‘
}

config[‘bitbucket.org‘] = {‘User‘:‘hg‘}

config[‘topsecret.server.com‘] = {‘Host Port‘:‘50022‘,‘ForwardX11‘:‘no‘}

with open(‘example.ini‘, ‘w‘) as configfile:

config.write(configfile)

View Code

查找文件

作者: liuzhihao

为您推荐

返回顶部