在python3中,configparser模块用于生成和修改常见配置文档,eg:mysql、apache虚拟主机等
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 print(''' 5 ========================= 6 configparser模块详解 7 ========================= 8 ''') 9 10 ''' 11 configparser 模块用于生成和修改常见配置文档,eg:mysql、apache虚拟主机等 12 ''' 13 14 print(''' 15 ============生成配置文件=========== 16 ''') 17 18 import configparser 19 20 config = configparser.ConfigParser() #首先定义一个空的配置文件描述体 21 config['DEFAULT'] = { 22 'ServerAliveInterval':'50', #在configparser模块生成配置文件时,值都是字符串(数字也要用引号) 23 'Compression':'yes', 24 'CompressionLevel':'7' 25 } #固定格式DEFAULT,定义全局变量(每个节点里面都有的,就可以写到这里,其他节点就不用写) 26 27 config['bitbucket.org'] = {} #定义一个新的空的节点['bitbucket.org'] 28 config['bitbucket.org']['User'] = 'hg' #在['bitbucket.org']节点下添加 User = 'hg'这个键值对 29 config['topsecret.server.com'] = {} 30 topsecret = config['topsecret.server.com'] 31 topsecret['Host Port'] = '65511' 32 topsecret['ForwardX11'] = 'no' 33 config['DEFAULT']['ForwardX11'] = 'yes' 34 with open('./file/example.ini', 'w') as configfile: 35 config.write(configfile) 36 37 print('生成的配置文件见./file/example.ini') 38 39 print(''' 40 ============打印生成的配置文件=========== 41 ''') 42 43 with open('./file/example.ini', 'r') as f: 44 for line in f: #迭代器的优势,节省内存 45 print(line) 46 47 48 print(''' 49 ============读取生成的配置文件的指定信息=========== 50 ''') 51 import configparser 52 config = configparser.ConfigParser() 53 print('执行config.sections()的结果:' ,config.sections()) 54 print('bitbucket.org' in config) 55 cr = config.read('./file/example.ini') #把整个文件内容读入config 56 print("config.read('./file/example.ini'):",cr) 57 print('bitbucket.org' in config) 58 print(config) 59 for i in config: 60 print(i) #DEFAULT 、bitbucket.org 、topsecret.server.com 61 print('执行config.sections()的结果:' ,config.sections()) 62 print(config['bitbucket.org']['user']) #hg 63 print(config['bitbucket.org']['compressionlevel']) # 7 (字符串) 64 topsecret = config['topsecret.server.com'] 65 print(topsecret['host port']) 66 for i in topsecret: 67 print(i) 68 ''' 69 host port 70 forwardx11 71 serveraliveinterval 72 compression 73 compressionlevel 74 75 ''' 76 77 78 print(''' 79 ============configparser增删改查语法=========== 80 ''') 81 82 import configparser 83 84 config = configparser.ConfigParser() 85 config.read('./file/example.ini') 86 87 # ########## 读 ########## 88 secs = config.sections() 89 print(secs) 90 #options = config.options('DEFAULT') # DEFAULT节点不能直接读,要用config.dafaults() 91 options = config.options('bitbucket.org') 92 # options()返回一个给定的名称的内容列表。 93 print(options) 94 p_DEFAULT = config.defaults() # DEFAULT节点不能直接读,要用config.dafaults() 95 print(p_DEFAULT) # 返回有序字典 96 #OrderedDict([('serveraliveinterval', '50'), ('compression', 'yes'), ('compressionlevel', '7'), ('forwardx11', 'yes')]) 97 98 item_list = config.items('bitbucket.org') 99 #config.items() 把传入节点名的内容按(键,值)元祖的方式返回在列表中 100 print(item_list) 101 #[('serveraliveinterval', '50'), ('compression', 'yes'), ('compressionlevel', '7'), ('forwardx11', 'yes'), ('user', 'hg')] 102 103 val1 = config.get('topsecret.server.com','compression') 104 print(val1) 105 val2 = config.getint('topsecret.server.com','compressionLevel') 106 print(val2) 107 108 # ########## 改写 ########## 109 ''' 110 config.remove_section('bitbucket.org') 删除指定节点(包括其内容) 111 config.has_section('xtsec') 判断配置文件中是否存在[xtsec]这个节点 112 config.add_section('xtsec') 往配置文件中添加[xtsec]这个节点 113 config.set('xtsec','port','65530') 往节点xtsec中内容添加 port=65531 ,若port存在,则将其值更新为'65530'(必须是字符串) 114 config.remove_option('xtsec','passwd') 删除指定节点中的指定内容 115 116 注:在每次操作完成后,要重新写入文件以保存,否则更改无效 117 config.write(open('./file/example1.ini', "w")) 118 119 ''' 120 sec = config.remove_section('bitbucket.org') 121 config.write(open('./file/example1.ini', "w")) 122 123 sec = config.has_section('xtsec') 124 #print(sec) #False 125 sec = config.add_section('xtsec') 126 config.write(open('./file/example1.ini', "w")) 127 128 config.set('xtsec','port','65531') #不存在就添加,存在就更新 129 config.set('xtsec','user','root') 130 config.set('xtsec','passwd','123456') 131 config.write(open('./file/example1.ini', "w")) 132 ''' 133 port = 65531 134 user = root 135 passwd = 123456 136 ''' 137 138 config.remove_option('xtsec','passwd') 139 config.write(open('./file/example1.ini', "w")) 140 ''' 141 port = 65531 142 user = root 143 '''
上面用到的配置文件:
生成的文件:example.ini
1 [DEFAULT] 2 serveraliveinterval = 50 3 compression = yes 4 compressionlevel = 7 5 forwardx11 = yes 6 7 [bitbucket.org] 8 user = hg 9 10 [topsecret.server.com] 11 host port = 65511 12 forwardx11 = no
修改后的文件: example1.ini
1 [DEFAULT] 2 serveraliveinterval = 50 3 compression = yes 4 compressionlevel = 7 5 forwardx11 = yes 6 7 [topsecret.server.com] 8 host port = 65511 9 forwardx11 = no 10 11 [xtsec] 12 port = 65531 13 user = root