標籤:刪除 版本 read str with open style color efault 設定檔
一.configparser模組
用於產生和修改常見配置文檔,但那個錢模組名稱在python3.x版本中變更為configparser。
1.產生一個配置。
import configparserconfig = configparser.ConfigParser()config["DEFAULT"] = {‘serveraliveinterval‘:‘45‘,‘compression‘:‘yes‘,‘compressionlevel‘:‘9‘}config[‘bitbucket.org‘] = {}config[‘bitbucket.org‘][‘user‘] = ‘hg‘with open(‘example.ini‘,‘w‘) as configfile:config.write(configfile)
注:組建組態檔案example.ini
2.讀取設定檔
import configparserconf = configparser.ConfigParser()conf.read("example.ini")print(conf.defaults())print(conf.sections())print(conf[‘bitbucket.org‘][‘user‘])
註:conf.defaults:讀取的是defaults以字典類型讀取
註:conf.sections:讀取的是節點,不包含defaults。
註:conf[‘bitbucket.org‘][‘user‘]:則是直接讀取節點下內容。
4.刪除設定檔內容。
import configparserconf = configparser.ConfigParser()conf.read("example.ini")print(conf.defaults())print(conf.sections())print(conf[‘bitbucket.org‘][‘user‘])sec = conf.remove_section(‘bitbucket.org‘)conf.write(open(‘exmple2.cfg‘,"w"))
註:刪除並建立備份新的檔案內。
Python configparser模組