![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import configparser #引入模块 2 3 '''config = configparser.ConfigParser() #类中一个方法 #实例化一个对象 4 5 config["DEFAULT"] = {'ServerAliveInterval': '45', 6 'Compression': 'yes', 7 'CompressionLevel': '9', 8 'ForwardX11':'yes' 9 } #类似于操作字典的形式 10 11 config['bitbucket.org'] = {'User':'Atlan'} #类似于操作字典的形式 12 13 config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} 14 15 with open('example.ini', 'w') as configfile: 16 17 config.write(configfile) ''' 18 config=configparser.ConfigParser() 19 config.read("example.ini") 20 b=config.sections() 21 c=config.defaults() 22 d="bitbucket.org" in b 23 #e=config[b[0]]["user"] 24 e=config["bitbucket.org"]["user"] 25 print(b) 26 print(c) 27 print(d) 28 print(e) 29 '''for key in config["bitbucket.org"]: 30 print(key)''' 31 for key2 in config: 32 print(key2) 33 ''' 34 config.remove_section("bitbucket.org") 35 #config.write(open("example2.ini","w")) 36 c=open("example.ini","w") 37 config.write(c)''' 38 39 config.set("topsecret.server.com","host port","aaaaa") 40 41 42 config.remove_option("topsecret.server.com","host port") 43 c=open("example.ini","w") 44 config.write(c)