-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_parser.py
41 lines (36 loc) · 1.39 KB
/
config_parser.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
from configparser import ConfigParser
def GetConfigPaster(section, key):
'''
Get a config parser value in the config.ini file.
Raise Excepton if can't find path to the file
section(string): the section of the needed config paster
key(string): the individual key of the config paster value
Returns: string
'''
config_file_path = 'config.ini'
config = ConfigParser()
try:
with open(config_file_path) as conf:
config.read(config_file_path)
return config.get(section, key)
except (OSError, IOError) as e:
raise Exception("Couldn't find path to config.ini.") from e
def ChangeConfigPasterValue(section, key, value):
'''
Change a config parser value in the config.ini file.
Raise Excepton if can't find path to the file
section(string): the section of the needed config paster
key(string): the individual key of the config paster value
value(string): the value need to be changed
Returns: None
'''
config_file_path = 'config.ini'
config = ConfigParser()
try:
with open(config_file_path) as conf:
config.read(config_file_path)
config[section][key] = value
with open(config_file_path, 'w') as conf:
config.write(conf)
except (OSError, IOError) as e:
raise Exception("Couldn't find path to config.ini.") from e