-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
35 lines (26 loc) · 992 Bytes
/
Copy pathconfig.py
File metadata and controls
35 lines (26 loc) · 992 Bytes
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
import configparser
class ConfigHandler:
def __init__(self, config_file='config.ini'):
self.config_file = config_file
self.config = configparser.ConfigParser()
self.load_config()
def load_config(self):
self.config.read(self.config_file)
def get_value(self, section, key):
if section in self.config and key in self.config[section]:
return self.config[section][key]
else:
return None
def set_value(self, section, key, value):
if not self.config.has_section(section):
self.config.add_section(section)
self.config.set(section, key, value)
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)
if __name__ == '__main__':
handler = ConfigHandler()
# 获取配置值
value = handler.get_value('new_section', 'new_key')
print(value)
# 设置配置值
handler.set_value('new_section', 'new_key', 'new_value')