-
Notifications
You must be signed in to change notification settings - Fork 187
/
read_write_conf.py
88 lines (62 loc) · 1.87 KB
/
read_write_conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# encoding:utf-8
# !/usr/bin/env python
# -*- Python读写conf格式配置文件 -*-
__author__ = 'tanteng'
import configparser
import webbrowser
# 返回config对象
conf = configparser.ConfigParser()
conf.read('./conf/demo.conf', 'utf-8')
# 读取配置文件
def readConf():
# 得到所有sections
sections = conf.sections()
print(sections)
# 得到sec_a,sec_b的设置项
options_sec_a = conf.options('sec_a')
print(options_sec_a)
options_sec_b = conf.options('sec_b')
print(options_sec_b)
# 得到sec_a,sec_b的设置键值对
items_sec_a = conf.items('sec_a')
print(items_sec_a)
items_sec_b = conf.items('sec_b')
print(items_sec_b)
# 得到具体某个section某个option的值
sec_a_key1 = conf.get('sec_a','a_key1')
print(sec_a_key1)
readConf()
'''
readConf()运行结果:
['sec_a', 'sec_b']
['a_key1', 'a_key2']
['b_key1', 'b_key2', 'b_key3', 'b_key4']
[('a_key1', '20'), ('a_key2', '10')]
[('b_key1', '121'), ('b_key2', 'b_value2'), ('b_key3', '$r'), ('b_key4', '127.0.0.1')]
20
'''
# 写配置文件
def writeConf():
# 更新某个section某个option的值
conf.set('sec_a','a_key1','100') # 最后一个参数必须是string类型
value = conf.get('sec_a','a_key1')
print(value) # 打印结果看是否设置成功
conf.add_section('new_section')
conf.set('new_section','new_option_name','new_option_value')
new_sections = conf.sections()
# 检测是否新增section和新增设置项成功
print(new_sections)
print(conf.get('new_section','new_option_name'))
writeConf()
'''
writeConf()运行结果:
100
['sec_a', 'sec_b', 'website', 'new_section']
new_option_value
tantengdeMacBook-Pro:learn-python tanteng$
'''
# 更多Python3入门文章,读取网址配置跳转,现学现用
def jump():
url = conf.get('website','url')
webbrowser.open(url)
jump()