Python Yaml Read and Write File

Jun 13, 2019
import osimport yamlYAML_FILE = 'config.yaml'# optional: create file it doesn't existif not os.path.exists(YAML_FILE):    with open(YAML_FILE, 'w'): pass# load yaml from filewith open(YAML_FILE, 'r') as f:    yaml_data = yaml.safe_load(f) or {}# do somethingyaml_data['test'] = ['apple', 'orange', 'pear']# write yaml to filewith open(YAML_FILE, 'w') as f:    # yaml.dump(yaml_data, f, default_flow_style=False)    yaml.safe_dump(yaml_data, f, default_flow_style=False) # , encoding='utf-8', allow_unicode=True

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.