Python Tip #311 (of 365): Make directories with parents and exist_ok. Like this:...

@treyhunner@mastodon.social

Python Tip #311 (of 365):

Make directories with parents and exist_ok.

Like this:
pathlib.Path("reports/2026").mkdir(parents=True, exist_ok=True)

Without "parents=True", the mkdir method raises an exception when parent directories are missing:

>>> reports = Path("reports/2026")
>>> reports.mkdir()
FileNotFoundError: [Errno 2] No such file or directory: 'reports/2026'

Passing "parents=True" will create missing parent directories.

๐Ÿงต (1/3)

#Python #DailyPythonTip