配置文件demo:
version: 2.2.8
sn: xxxx
task: idle
import yaml
读取配置文件内容
# item 为 key (task: idle中的task)
def read(file, item):
with open(file, 'r', encoding='utf-8') as f:
file_content = f.read()
f.close()
content = yaml.load(file_content, yaml.FullLoader)
value = content[item]
return value
修改配置文件内容:
def edit(file, item, value):
with open(file, 'r', encoding='utf-8') as f:
file_content = f.read()
f.close()
content = yaml.load(file_content, yaml.FullLoader)
content[item] = value
with open(file, 'w', encoding='utf-8') as f:
yaml.dump(content, f, allow_unicode=True)
f.close()