def ensure_directory(path):
Check if the path exists and if it does not create the directory
directory = os.path.split(path)[0]
if directory and notos.path.exists(directory):
os.makedirs(directory)
After Change
Check if the path exists and if it does not, creates the directory.
// If it"s a file path, try the parent directory instead
p = Path(path)
p = p.parent if p.is_file() else p
try:
p.mkdir(parents=True, exist_ok=False)