configparser模块
用python生成一个config文档
1 config = configparser.ConfigParser() # 创建一个configparser对象
2 config['DEFAULT'] = {
3 'ServerAliveInterval': '45',
4 'Compression': 'yes',
5 'CompressionLevel': '9'
6 }
7 config['bitbucket.org'] = {}
8 config['bitbucket.org']['User'] = 'hg'
9 config['topsecret.server.com'] = {}
10 topsecret = config['topsecret.server.com']
11 topsecret['Host Port'] = '50022'
12 topsecret['ForwardX11'] = 'no'
13 config['DEFAULT']['ForwardX11'] = 'yes'
14 with open('example.ini','w') as configfile:
15 config.write(configfile)
增删改查
1 # 增删改查
2 # config = configparser.ConfigParser()
3 # print(config.sections())# []
4 # config.read('example.ini')
5 # print(config.sections())
6
7 # for k in config.sections():
8 # print(k)
9
10 # for k,v in config['bitbucket.org'].items():
11 # print(k,v)
12
13 # if 'user' in config['bitbucket.org']:
14 # print('in')
15
16 # print(config['bitbucket.org']['user'])
17
18 # print(dir(config))
19
20 # print(config.options('topsecret.server.com'))
21
22 # print(config['topsecret.server.com']['k1'])
23
24 # config['topsecret.server.com']['name'] = '22'
25 # config['topsecret.server.com']['age'] = '22'
26 # config.write(open('example2.ini','w'))
27
28 # config.set('topsecret.server.com','host port','456798')
29 # config.write(open('example3.ini','w'))
30
31 # config.remove_option('topsecret.server.com','host port')
32 # config.write(open('example4.ini','w'))