Python Create/Make Directories Before File Write

October 19, 2019
os.makedirs
import os

file_path = '/tmp/test'
content = 'Hello World'

#  make sure dir exist
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'wb') as f:
    f.write(content)
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.