這個模塊是python標準庫模塊,從2.6.6開始引入,用來解析配置文件。
## 直接使用
#### 實例化
```
import ConfigParser
config_obj = ConfigParser.ConfigParser()
```
---
#### 讀取配置文件
```
# 讀取文件對象
config_obj.readfp(fo)
# 讀取文件
config_obj.read()
```
#### 寫配置文件,文件對象
```
config_obj.write(fo)
```
---
#### 判斷option是否存在
```
config_obj.has_section(section)
```
#### 獲取sections
```
config_obj.sections()
```
#### 添加section
```
config_obj.add_section
```
#### 移除section
```
config_obj.remove_section(section)
```
---
#### 判斷option是否存在
```
config_obj.has_option(section, option)
```
#### 獲取options
```
config_obj.options(section)
```
#### 獲取值
```
config_obj.get(section, option)
```
#### 獲取(key/value) tuple
```
config_obj.items(section)
```
#### 添加option
```
config_obj.set(section, option, value=None)
```
#### 刪除option
```
config_obj.remove_option(section, option)
```
---
## 定制類
```
#!/usr/bin/env python
# encoding: utf-8
from ConfigParser import ConfigParser
class MysqlConfig(ConfigParser):
def __init__(self, config_file, **kwargs):
ConfigParser.__init__(self, allow_no_value=True)
# super(MysqlConfig, self).__init__(allow_no_value=True)
self.config_file = config_file
self.kwargs = kwargs
# 初始化時就執行讀取配置文件操作
self.read(self.config_file)
self.set_vars(self.kwargs)
self.get_vars('mysqld')
def set_vars(self, kwargs):
"設置類屬性"
for k, v in kwargs.items():
setattr(self, k, v)
def get_vars(self, section='mysqld'):
"""讀取配置文件的參數存為類屬性,默認是mysqld"""
attr_dict = {}
options = self.options(section)
for item in options:
attr_dict[item] = self.get(section, item)
self.set_vars(attr_dict)
# def get_vars(self, section='mysqld'):
# attr_dict = dict(self.items(section))
# self.set_vars(attr_dict)
if __name__ == '__main__':
conf_obj = MysqlConfig('/usr/share/doc/mysql-server-5.1.73/my-huge.cnf')
print conf_obj.items('mysqld')
```
- 前言
- 環境搭建
- pypi
- 打包
- Python 2 和 Python 3 的版本之間差別
- 項目
- 第一部分
- 第1章 基礎
- Python安裝
- python代碼文件類型
- python對象
- 核心數據類型
- 核心數據類型--整型和浮點型
- 核心數據類型--字符串
- str.format
- 核心數據類型--列表
- 核心數據類型--元組
- 核心數據類型--字典
- 核心數據類型--集合
- 核心數據類型--文件對象
- 調用bash
- 標準輸入輸出
- str-repr
- 字符編碼
- 迭代器和生成器
- 第2章 語句和語法
- 賦值語句
- if語句
- while語句
- for語句
- assert
- 第3章 函數
- 函數作用域
- 工廠函數
- 內置函數
- 遞歸
- 嵌套作用域和lambda
- 參數傳遞
- 函數式編程
- property可寫與可讀
- 第5章 模塊
- 模塊導入
- 模塊命名空間
- 相對導入和絕對導入
- 模塊重載
- 在模塊中隱藏數據
- 過渡性重載
- 第6章 類
- 面向對象還是面向過程?
- 構造函數 析構函數
- call
- 運算符重載
- str()
- 待定
- 即時生成屬性
- 多態
- 線程和進程
- thread模塊
- threading模塊
- threading線程鎖
- 糖果機
- multiprocessing
- 阻塞非阻塞同步異步
- 單線程和多線程對比
- 生產者消費者模型
- 第二部分
- 獲取系統資源信息
- 獲取進程所占的物理內存
- dmidecode獲取系統信息
- 網絡編程
- 網絡基礎
- python中的套接字
- socket模塊
- 第三部分 高級功能
- 閉包入門
- 閉包的應用
- 裝飾器入門
- 裝飾器應用
- 第四部分 項目實戰
- graphite
- 模塊
- collections
- datetime
- Enum
- faker
- fabric
- fileinput
- fire
- fnmatch
- getpass
- glob
- hashlib
- heapq
- json模塊
- log
- os
- Paramiko
- parser
- platform
- pyyaml
- Queue
- random
- re
- 特殊符號和字符
- re模塊
- shelves
- subprocess
- time
- urllib_urllib2_requests
- urllib urllib2
- requests
- 標準模塊ConfigParser
- 擴展模塊Mysqldb
- 擴展模塊dns
- 擴展模塊request
- uuid
- cacheout 緩存庫
- delorean 時間
- 附錄
- 內置函數
- python實現各種排序算法
- 常見報錯
- pymongo
- pyrocksdb
- 常用
- ERROR