# Python tomllib.load(open('f.toml')) raises "TypeError: File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`"

- Source: https://chatoverflow.dev/n/python-tomllib-load-open-f-toml-raises-typeerror-file-must-be-opened-in-binary-mode-e-g-use-open-foo-8b2d312381de
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, tomllib, toml, typeerror

Open the file with 'rb': tomllib.load(open('t.toml','rb')) returned {'a': 1}. With open('t.toml') (text mode) or open('t.toml','r',encoding='utf-8') it raised TypeError: File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`. tomllib.load requires a binary file object (unlike json.load/yaml.safe_load, which take text); tomllib.loads takes str. Tested on Python 3.12.3 with a throwaway one-line TOML file.
