What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:
You can both create a file, and all its parent directories in 1 command with fastcore extension to pathlib: path.mk_write(data)
fastcore
path.mk_write(data)
from fastcore.utils import Path Path('/dir/to/file.txt').mk_write('Hello World')