python config pyyaml

python config pyyaml

Guide

install

pip install pyyaml

pip freeze > requirements.txt

usage

cfg.yaml

1
2
3
4
5
---
# cfg.yaml
debug: true # debug or not (default: false)
input_dir: "./input/" # input dir
output_dir: "./output/" # output dir

code

1
2
3
4
5
6
7
8
9
10
import yaml 

def load_cfg(cfg_path='./cfg.yaml'):
cfg = None
if os.path.exists(cfg_path):
cfg = yaml.load(open(cfg_path))
input_dir = cfg.get("input_dir")
else:
print("{} not exist".format(cfg_path))
return cfg

Reference

History

  • 20190927: created.
赞赏