Python常用模块-configparser配置模块
Python常用模块-configparser配置模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
来看一个好多软件的常见文档格式如下
例如不全面的MySQL 配置文件my.cnf
[client] port = 3306 socket=/home/admin/mysql_data/mysql.sock [mysqld] lower_case_table_names = 1 server-id = 1 port = 3306 [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout .........
生成文件
import configparser config = configparser.ConfigParser() config["client"] = {'port': '3306', 'socket' : '/home/admin/mysql_data/mysql.sock', } config['mysqld'] = {} config['mysqld']['lower_case_table_names'] = '1' config['mysqld']['server-id'] = '1' config['mysqld']['port'] = '3306' config['myisamchk'] = {} chk = config['myisamchk'] chk['key_buffer_size'] = '128M' chk['sort_buffer_size'] = '128M' chk['read_buffer'] = '2M' chk['write_buffer'] = '2M' with open('my.cnf','w') as configfile: config.write(configfile)
[client]
port = 3306
socket = /home/admin/mysql_data/mysql.sock
[mysqld]
lower_case_table_names = 1
server-id = 1
port = 3306
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
读出文件
MacBook-Air:configparser admin$ python3
Python 3.6.0rc2 (v3.6.0rc2:800a67f7806d, Dec 16 2016, 14:12:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('my.conf')
[]
>>> config.sections()
[]
>>> config.read('/Users/admin/PycharmProjects/s18/day5/模块/configparser/my.cnf')
['/Users/admin/PycharmProjects/s18/day5/模块/configparser/my.cnf']
>>> config.sections()
['client', 'mysqld', 'myisamchk']
>>> 'client' in config
True
>>> 'client2' in config
False
>>> config['client']
<Section: client>
>>> config['client']['port']
'3306'
>>>
>>> ret = config['myisamchk']
>>> ret['read_buffer']
'2M'
>>>
>>> config['mysqld']['server-id']
'1'
>>> for k in config['mysqld']:
... print(k)
...
lower_case_table_names
server-id
port
>>>
>>> ret = config.sections()
>>> print(ret)
['client', 'mysqld', 'myisamchk']
>>> options = config.options('mysqld')
>>> print(options)
['lower_case_table_names', 'server-id', 'port']
>>> item_list = config.items('mysqld')
>>> print(item_list)
[('lower_case_table_names', '1'), ('server-id', '1'), ('port', '3306')]
>>> val = config.get('mysqld','port')
>>> print(val)
3306
>>> val = config.getint('mysqld','port')
>>> print(val)
3306
>>>
删除
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read('my.cnf')
['my.cnf']
>>> config.sections()
['client', 'mysqld', 'myisamchk']
>>> config.options('mysqld')
['lower_case_table_names', 'server-id', 'port']
>>>
>>> ret = config.remove_section('client')
>>> config.write(open('my.cnf_2','w'))
>>>
[mysqld]
lower_case_table_names = 1
server-id = 1
port = 3306
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
增加
>>> set = config.has_section('wangxu')
>>> print(set)
False
>>> config.add_section('wangxu')
>>> config.write(open('my.cnf_wangxu','w'))
>>>
[mysqld]
lower_case_table_names = 1
server-id = 1
port = 3306
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[wangxu]
>>> config.set('wangxu','age','28')
>>> config.write(open('my.cnf_wangxu','w'))
>>>
[mysqld]
lower_case_table_names = 1
server-id = 1
port = 3306
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[wangxu]
age = 28
修改
>>> config.set('wangxu','age','29')
>>> config.write(open('my.cnf_wangxu','w'))
>>>
[mysqld]
lower_case_table_names = 1
server-id = 1
port = 3306
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[wangxu]
age = 29