Python 配置文件的操作
2015-04-30 14:41
465 查看
读取配置文件的特定section和option
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
host = conf.get('mysql', 'host')
print host
写入特定section的option和value
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.add_section('newsection')
conf.set('newsection', 'newoption', 'newvalue')
f = open('bsrom.cfg','a+')
conf.write(f)
f.close()
修改
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
conf.set('romkeyword', 'filepath', 'path2')
f = open('bsrom.cfg','r+') #注意修改是r+模式
conf.write(f)
f.close()
删除
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
conf.remove_option('common', 'log_path') #删除配置项
conf.remove_section('beanstalkc')
f = open('bsrom.cfg','w+') #注意打开模式
conf.write(f)
f.close()
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
host = conf.get('mysql', 'host')
print host
写入特定section的option和value
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.add_section('newsection')
conf.set('newsection', 'newoption', 'newvalue')
f = open('bsrom.cfg','a+')
conf.write(f)
f.close()
修改
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
conf.set('romkeyword', 'filepath', 'path2')
f = open('bsrom.cfg','r+') #注意修改是r+模式
conf.write(f)
f.close()
删除
#!/usr/bin/python
# -*- coding:UTF-8 -*-
'''
Created on 2015-4-30
@author: huangpeng03
'''
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('bsrom.cfg')
conf.remove_option('common', 'log_path') #删除配置项
conf.remove_section('beanstalkc')
f = open('bsrom.cfg','w+') #注意打开模式
conf.write(f)
f.close()
相关文章推荐
- python 中对配置文件(如.ini文件)进行读写等操作ConfigParser模块
- python操作配置文件
- Python读写配置文件的实际操作步骤解析
- Python读写配置文件的实际操作步骤解析
- python操作conf配置文件
- Python 操作配置文件
- python 提供INI配置文件的操作 ConfigParser
- python配置文件操作——configparser模块
- Python读写配置文件的实际操作步骤解析
- Python中利用ConfigParser操作配置文件
- python配置文件操作
- Python 中配置文件处理详解--ConfigParser,有了它就可随意操作配置文件了
- python 操作haproxy配置文件
- python 3 ini配置文件操作
- python 操作haproxy配置文件
- python操作配置文件ini的方法
- Python模块configparser(操作配置文件ini)
- Python学习之配置文件操作
- Python读取properties配置文件操作示例
- Python的Django安装nginx1.9.3以上版本修改配置文件的操作