python3 中需要使用引入新版的configparser,而非ConfigParser
讀寫前都要用read()打開配置文件,假如用add_section()就直接涼了,因爲後者會直接清空配置文件,只留下一條你在後面set()的鍵值對。
1.如果要更新一個section:
conf = configparser.configparser()
conf.add_section(‘purchasecar’)
conf.set(‘purchasecar’, ‘count’, ‘1’)
2.讀取一個鍵值對:
conf = configparser.configparser()
conf.read(self.file_name)
conf.get(self.node, key)
3.更新鍵值對
conf = configparser.configparser()
conf.read(self.file_name)
confset(self.node, key, value)
confwrite(open("./config/config.ini", “w”))