Why must you use `yaml.safe_load` rather than `yaml.load`?
BeginnerConceptData formats, HTTP & REST APIs
Answer
PyYAML's full loader can construct arbitrary Python objects from tags like !!python/object/apply:os.system, so loading an untrusted YAML file can execute code. yaml.safe_load only builds plain types (dict, list, str, int, float, bool, None, dates). Since PyYAML 5.1, calling yaml.load without a Loader warns, and 6.0 made the argument required. For writing, yaml.safe_dump(data, sort_keys=False).
More on Data formats, HTTP & REST APIs
How do you read and write JSON, and what are the common gotchas?BeginnerHow do you load a multi-document Kubernetes YAML file?IntermediateHow do you read and write CSV files properly?BeginnerHow do you make an HTTP request with `requests` the right way?BeginnerWhy use a `requests.Session`?IntermediateHow do you add retries with exponential backoff to `requests`?Intermediate