import configparser
config = configparser.ConfigParser()
#在DEFAULT節(jié)點(diǎn)下添加,類似字典的key和value
config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'}
config['bitbucket.org'] = {"alex":33}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes' #在在DEFAULT節(jié)點(diǎn)下添加key為'ForwardX11,value為yes的數(shù)據(jù)
with open('example.ini', 'w') as configfile:
config.write(configfile)
#serveraliveinterval = 45
#compression = yes
#compressionlevel = 9
#forwardx11 = yes
#[bitbucket.org]
#alex = 33
#user = hg
#[topsecret.server.com]
#host port = 50022
#forwardx11 = no
import configparser
conf=configparser.ConfigParser()
conf.read("example.ini")
print(conf.sections())#打印節(jié)點(diǎn)(除了“DEFAULT”以外的節(jié)點(diǎn))
print(conf["bitbucket.org"]["user"])
print(conf.defaults())#打印[DEFAULT]
#刪除后,創(chuàng)建新文件寫入
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.cfg', "w"))
#刪除后,覆蓋原文件
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.ini', "w"))
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。