[root@linag python]# vim config.py import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'Root' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '5200' topsecret['Forward'] = 'yes' config['DEFAULT']['Forward'] = 'No' with open('example.ini','w') as configfile: config.write(configfile) config.set('topsecret.server.com','Hosr Port','5601') #改 config.remove_section('topsecret.server.com') #删 config.write(open ('xml.ini','w')) [root@linag python]# python config.py [root@linag python]# cat example.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forward = No [bitbucket.org] user = Root [topsecret.server.com] host port = 5200 forward = yes [root@linag python]# cat xml.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forward = No [bitbucket.org] user = Root