Provide Directory
The `provide-dir` library is a lightweight utility that ensures a given directory path exists, creating all necessary parent directories if they don't. It's built on top of `os.makedirs` and offers a simplified interface. Currently at version 0.1.2, it follows an infrequent but steady release cadence, with updates typically several months apart.
Warnings
- gotcha The `mode` argument (permissions) defaults to `0o777` (world-writable), which might be too permissive for certain environments or security requirements. Always explicitly set `mode` (e.g., `0o755`) if specific permissions are needed.
- gotcha If `exist_ok` is set to `False` (the default is `True`), a `FileExistsError` will be raised if the target directory already exists. This can be unexpected if you assume it will always succeed for existing paths.
- gotcha This function is designed to create directories. If you provide a path that is intended to be a *file* (e.g., `path/to/file.txt`), `provide_dir` will create `path/to/file.txt` as a *directory*, not just its parent `path/to`. To create parent directories for a file, use `os.path.dirname` first.
Install
-
pip install provide-dir
Imports
- provide_dir
from provide_dir import provide_dir
Quickstart
from provide_dir import provide_dir
import os
# Define a test directory path
base_dir = os.environ.get('TEST_BASE_DIR', 'temp_test_dir')
dir_path = os.path.join(base_dir, 'my', 'nested', 'directory')
print(f"Attempting to create: {dir_path}")
# Create the directory and all its parents
created_path = provide_dir(dir_path)
print(f"Directory ensured at: {created_path}")
print(f"Does the directory exist? {os.path.isdir(created_path)}")
# Clean up (optional)
# import shutil
# shutil.rmtree(base_dir)