Skip to content

Filesystem reference


temp_directory

temp_directory() -> Generator[Path, None, None]

A context manager that creates a temporary directory and yields a path to it. Example:

with temp_directory() as temp_dir:
    ...

Yields:

Type Description
Path

The resolved path to the temporary directory, following all symlinks.

Path

Bases: Path

long_id

long_id: str

Returns a SHA-256 hashed, URL-safe base64 encoded representation of the current path. This is useful on case-insensitive filesystems to identify paths that are the same.

Caveat

This identifier considers the filesystem to be case-insensitive on macOS. Although that is not a technical guarantee, it is in practice true.

Returns:

Type Description
str

A unique identifier for the current path.

id

id: str

Returns:

Type Description
str

The first 8 characters of the long ID.

ensure_dir

ensure_dir() -> None

Ensure the current path is a directory. Equivalent to calling Path.mkdir with parents=True and exist_ok=True.

expand

expand() -> Path

Expand the current path by resolving the user home directory and environment variables.

Returns:

Type Description
Path

The new expanded path.

write_atomic

write_atomic(data: str | bytes, *args: Any, **kwargs: Any) -> None

Atomically write data to the current path.

Parameters:

Name Type Description Default
data str | bytes

The data to write.

required

Other Parameters:

Name Type Description
*args Any

Additional arguments to pass to os.fdopen.

**kwargs Any

Additional keyword arguments to pass to os.fdopen.

as_cwd

as_cwd() -> Generator[Path, None, None]

A context manager that changes the current working directory to the current path. Example:

with Path("foo").as_cwd():
    ...

Yields:

Type Description
Path

The current path.