When running programs in Python, I often like to put my output into a folder based on the current time. The following is a code snippet to ensure a file path exists (and logs the folder creation).
import logging
import os
def create_path(file):
"""Creates a path if it does not exist
:param file: the path/file that we want to check if it exists
"""
directory, file = os.path.dirname(file), os.path.basename(file)
if not os.path.exists(directory):
logging.debug(f"Path {directory} does not exist for {file}, creating it now!")
os.makedirs(directory)