配置文件是常用文件,配置文件格式分为:文本文件,ini文件,xml文件等;这里主要讲下ini配置文件管理。
ini文件
ini文件主要分下面三部分: 1:ini文件格式; 2:ini相关模块操作; 3:实际案例:使用Python修改mysql配置文件;
ini文件格式介绍:
ini文件是一种常用配置文件,由节、键、值组成;文件格式如下: section] # 节 key=value # key:键,value为值 一个配置文件中可以有多个section,每个section名字不能相同,每个section下可以分多个键值,每个section下的键不能相同;例如:Mysql配置文件格式为ini文件,部分内容如下: [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql bind-address = 127.0.0.1 key_buffer_size = 16M log_error = /var/log/mysql/error.log
configparser模块详解:
configparser是Python中自带模块,主要用于操作ini配置文件; 读操作方法如下: 方法 说明 config = configparser.ConfigParser() 创建 ConfigParser对象 config.read(filenames, encoding=None) 读取配置文件 config.sections() 获取所有的section config.options(section) 获取指定section下所有的key config.get(section, option,...) 获取指定section下指定key的值 config.items(section,...) 获取指定section下所有key与value
我们尝试使用configparse模块读取mysql数据库内容: 1:获取所有section, 2:获取mysqld_safe下所有key, 3:获取mysqld_safe下所有key-value, 4:获取mysqld下log_error对应的值;
大家可以复制mysql配置文件内容,然后使用configparse进行读取,代码实现如下: 导入模块 import configparser #配置文件路径: path = '/home/ Linux /test/mysqld.cnf' #创建ConfigParser模块 config = configparser.ConfigParser() #读取文件 config.read(path, encoding='utf-8') #获取所有的section sections = config.sections() print(sections) #获取section为mysqld_safe下所有key keys = config.options('mysqld_safe') print(keys) #获取section为mysqld_safe下所有的key-value items = config.items('mysqld_safe') print(items) #获取section为mysqld,key为log_error对应的value val = config.get('mysqld', 'log_error') print(val)
输出如下: ['mysqld_safe', 'mysqld'] ['socket', 'nice'] [('socket', '/var/run/mysqld/mysqld.sock'), ('nice', '0')] /var/log/mysql/error.log
对于配置文件,我们不仅需要读取,而且还需要修改,下面我们看下如何修改配置文件。 修改方法如下: 方法 说明 config.set(section, option, value=None) 设置section中指定key的value值,key不存在,直接添加key-value config.add_section(section) 配置信息中添加section config.remove_section(section) 删除section config.remove_option 删除指定section中key config.write(fp, space_around_delimiters=True) 将配置信息写回到配置文件
检查section与key的方法: 方法 说明 config.has_section(section) 检查指定section是否存在 config.has_option(section, option) 检查指定section下option是否存在 下面我来对修改mysql配置文件: 1:修改前判断mysqld是否存在,不存在添加section:mysqld, 2:设置log_error对应值:/home/workdir/log/mysql_error.log, 3:添加section:mysqldump(添加前需要判断是否已经存在), 4:在mysqldump中添加key:max_allowed_packet,对应值为:16M; 代码实现如下: import configparser #配置文件路径 path = '/home/ Linux /test/mysqld.cnf' #新配置文件 newpath = '/home/ Linux /test/mysqld1.cnf' #读取配置文件 config = configparser.ConfigParser() config.read(path) #是否存在section:mysqld if not config.has_section('mysqld'): config.add_section('mysqld') #设置logerror路径 config.set('mysqld', 'log_error','/home/workdir/log/mysql_error.log') #添加section:Mysqldump if not config.has_section('mysqldump'): config.add_section('mysqldump') config.set('mysqld','max_allowed_packet','16M') #打开要写入新的配置文件路径 wf = open(newpath,'w') #写入文件 config.write(wf, space_around_delimiters=True) wf.close() 然后可以打开文件,新的配置文件是否生成,创建文件是否有效。
查看更多关于python ini配置文件管理的详细内容...