Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
from typing import Optional

def create_directory(path: str, permission: Optional[int] = None) -> None:
"""
Creates a new directory at the specified path.

:param path: The path to create the directory.
:param permission: The permissions to set on the directory. If None, the
default permissions for the current user and system
will be used.
:raises FileExistsError: If the directory already exists.
:raises PermissionError: If there is a permission error when trying to
create the directory.
:raises ValueError: If the path is not a string.
"""
if not isinstance(path, str):
raise ValueError("Path must be a string.")

if not os.path.exists(path):
os.makedirs(path, exist_ok=False, mode=permission)
elif os.path.isfile(path):
raise PermissionError(f"Cannot create directory at path {path}: "
"A file with the same name already exists.")
else:
raise FileExistsError(f"Directory already exists at path {path}.")