python configparser写入
时间: 2023-10-19 14:09:22 浏览: 41
使用 Python 的 configparser 模块可以很方便地写入配置文件。下面是一个简单的示例:
```python
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
```
在这个示例中,我们首先创建了一个 ConfigParser 对象,然后设置了一些默认值和一些特定节的值。最后,我们将配置写入文件 example.ini 中。