配置文件
用户配置文件就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置等。
该模块的作用 就是使用模块中的RawConfigParser()
、ConfigParser()
、 SafeConfigParser()
这三个方法(三者择其一),创建一个对象,使用对象的方法对指定的配置文件做增删改查 操作。
模块所解析的ini配置文件是由多个section构成,每个section名用中括号‘[]’包含,每个section下可有多个配置项类似于key-value形式,例如:
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
-
ConfigParser 初始化
使用ConfigParser 首选需要初始化实例,并读取配置文件:
cf = ConfigParser.ConfigParser() cf.read("配置文件名")
-
基本的读取配置文件
read(filename)
直接读取ini文件内容sections()
得到所有的section,并以列表的形式返回options(section)
得到该section的所有optionitems(section)
得到该section的所有键值对get(section,option)
得到section中option的值,返回为string类型getint(section,option)
得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
3.基本的写入配置文件
- add_section(section) 添加一个新的section
- set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
用python生成一个配置文件
import configparser
#第一种创建方式
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
#第二种创建方式
config['bitbucket.org'] = {}
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'
#文件的写入
with open('example.ini', 'w') as configfile:
config.write(configfile)
读配置文件
- 获得配置文件的分块
cp=configparser.ConfigParser()
cp.read('example.ini')
print(cp.sections())
#输出:['bitbucket.org', 'topsecret.server.com']
- 获得配置文件中的某一特定分块的key值
print(cp.options('topsecret.server.com'))
#输出:['host port', 'forwardx11']
- 获得配置文件中的某一特定分块的value值,返回一个列表,元素是元组(key,value)
print(cp.items('topsecret.server.com'))
#输出:[('host port', '50022'), ('forwardx11', 'no')]
- 获得配置文件中具体某条配置信息
print(cp.get('topsecret.server.com','host port'))
#输出:50022
- 写配置文件到某一分块
cp.set('bitbucket.org','test','123')
cp.write(open('example.ini','w'))
- 新增某一分块
cp.add_section('hah')
cp.write(open('example.ini','w'))
- 删除某一分块中配置
cp.remove_option('bitbucket.org','user')
cp.write(open('example.ini','w'))
- 删除某一分块注意,删除某一分块会连其下的配置全部删除。
cp.remove_section('bitbucket.org')
cp.write(open('example.ini','w'))
其它
import configparser
config = configparser.ConfigParser()
#----------------------查
print(config.sections()) #[]
config.read('example.ini')
print(config.sections()) #['bitbucket.org', 'topsecret.server.com']
print('bytebong.com' in config)# False
print(config['bitbucket.org']['User']) # hg
print(config['DEFAULT']['Compression']) #yes
print(config['topsecret.server.com']['ForwardX11']) #no
for key in config['bitbucket.org']:
print(key)
# user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11
print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
print(config.get('bitbucket.org','compression'))#yes
#--------------------------删,改,增(config.write(open('i.cfg', "w")))
config.add_section('yuan')
config.remove_section('topsecret.server.com')
config.remove_option('bitbucket.org','user')
config.set('bitbucket.org','k1','11111')
config.write(open('i.cfg', "w"))