FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learn-python/read_write_conf.py at master · tigerruncode/learn-python · GitHub
tigerruncode
/
learn-python
Public
forked from
tanteng/learn-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
learn-python
/
read_write_conf.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
88 lines (62 loc) · 1.87 KB
Breadcrumbs
learn-python
/
read_write_conf.py
Copy path
File metadata and controls
88 lines (62 loc) · 1.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
()
Back
|
FazBrowse Home
|
New Git URL