Skip to main content

How to Read a YAML File in Python with yaml.safe_load

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

YAML is a common format for settings files, because people can read and edit it easily. In Python, the PyYAML package reads one with yaml.safe_load, which turns the file into dictionaries, lists, text and numbers. This guide reads a small settings file with PyYAML 6.0.4 on Python 3.15.2.

Syntax​

Syntax
import yaml

with open("<file>.yaml", encoding="utf-8") as file:
<data> = yaml.safe_load(file)
PartWhat it means
import yamlLoads PyYAML. The package is called pyyaml, but the module you import is yaml
open("<file>.yaml", encoding="utf-8")Opens the file as text
yaml.safe_load(file)Reads the file and returns plain Python values: a dictionary for key: value lines

The PyYAML documentation says: "safe_load(stream) parses the given stream and returns a Python object constructed from for the first document in the stream." It adds that it "recognizes only standard YAML tags and cannot construct an arbitrary Python object."

The Example​

A file called shop.yaml holds three settings for a bike shop: its name, its opening hours and whether it delivers. A script reads the file and uses one of the numbers.

Prerequisites​

  • PyYAML in your project, for example with uv add pyyaml. This guide uses PyYAML 6.0.4 on Python 3.15.2.

Step 1: Create the File and the Script​

Save this as shop.yaml:

shop.yaml
name: Bike shop
open_hours: 8
delivery: true

Then the script, in the same folder:

read_yaml.py
import yaml

with open("shop.yaml", encoding="utf-8") as file:
settings = yaml.safe_load(file)

print(settings)
print(settings["open_hours"] + 1)

The read_yaml.py script in a macOS-style code window: import yaml, open shop.yaml as file, settings = yaml.safe_load(file), then print the settings and open_hours plus one.

Step 2: Run It​

Terminal
uv run python read_yaml.py
Output
{'name': 'Bike shop', 'open_hours': 8, 'delivery': True}
9

The terminal after uv run python read_yaml.py: the dictionary with name Bike shop, open_hours 8 and delivery True, then 9.

The file became a dictionary. 8 came back as a number, so open_hours + 1 gave 9, and true became Python's True. The command works unchanged in PowerShell.

Why safe_load and Not load​

yaml.load can build any Python object a file asks for, so a file from someone else could make it run code. The PyYAML documentation warns: "It is not safe to call yaml.load with any data received from an untrusted source!" safe_load refuses such tags. Given the text pick: !!python/object/apply:os.getcwd [], it stopped with:

Output (last lines)
yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:os.getcwd'
in "<unicode string>", line 1, column 7:
pick: !!python/object/apply:os.getcwd []
^

Common Mistakes​

MistakeWhat happensDo this instead
Calling yaml.load(file)TypeError: load() missing 1 required positional argument: 'Loader'Use yaml.safe_load(file)

Troubleshooting​

SymptomLikely causeFix
yaml.safe_load returns NoneThe file is empty: an empty .yaml file printed NoneCheck that the file has content
could not determine a constructor for the tagThe file uses a Python-specific tag such as !!python/objectKeep files to plain YAML values; only load such tags from files you wrote yourself

FAQ​

What does safe_load return for a list in the file?​

A Python list. YAML mappings become dictionaries, sequences become lists, and plain values become strings, numbers, True, False or None.

Does safe_load read more than one document?​

No, only the first. yaml.safe_load_all reads every document in a file that separates them with ---.

Conclusion​

yaml.safe_load turned shop.yaml into a dictionary with a number and a True in it. It reads plain YAML only, which is what a settings file needs, and it refused a tag that would have run code.

Additional Resources​