Skip to content

bagof.paths.Path

Path(path: Any, *, driver: Any = None, storage_options: Mapping[str, Any] | None = None)

Bases: PurePathMixin, BaseWrapper

A path that works like pathlib.Path, local or in the cloud.

Make one from a path string, a URL, or any path object:

>>> from bagof.paths import Path
>>> p = Path("/data/sets/train.zarr")
>>> p.name
'train.zarr'
>>> p.parent
Path('/data/sets')
>>> (p / "chunks").suffix
''

Each method uses the underlying library when it can, builds the method from simpler ones when it cannot, or raises UnsupportedPathOperation when neither is possible.

Methods:

exists

exists(*, follow_symlinks: bool = True) -> bool

Whether the path exists.

is_file

is_file(*, follow_symlinks: bool = True) -> bool

Whether the path is a regular file.

is_dir

is_dir(*, follow_symlinks: bool = True) -> bool

Whether the path is a directory.

is_symlink() -> bool

Whether the path is a symbolic link.

stat

stat(*, follow_symlinks: bool = True) -> stat_result

The result of stat on the path.

lstat

lstat() -> stat_result

Like :meth:stat, without following symbolic links.

samefile

samefile(other: Any) -> bool

Whether the path and other refer to the same file.

is_mount

is_mount() -> bool

Whether the path is a mount point.

is_socket

is_socket() -> bool

Whether the path is a Unix domain socket.

is_fifo

is_fifo() -> bool

Whether the path is a FIFO (named pipe).

is_block_device

is_block_device() -> bool

Whether the path is a block device.

is_char_device

is_char_device() -> bool

Whether the path is a character device.

is_junction

is_junction() -> bool

Whether the path is a junction (a Windows concept; else False).

open

open(mode: str = 'r', buffering: int = -1, encoding: str | None = None, errors: str | None = None, newline: str | None = None) -> IO[Any]

Open the path and return a file object, like :func:open.

read_bytes

read_bytes() -> bytes

Read the whole file as bytes.

read_text

read_text(encoding: str | None = None, errors: str | None = None, newline: str | None = None) -> str

Read the whole file as text.

write_bytes

write_bytes(data: Any) -> int

Write data to the file as bytes, replacing any content.

write_text

write_text(data: str, encoding: str | None = None, errors: str | None = None, newline: str | None = None) -> int

Write data to the file as text, replacing any content.

iterdir

iterdir() -> Iterator[Self]

Yield the paths of the directory's entries.

glob

glob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: bool = False) -> Iterator[Self]

Yield the paths matching pattern under this directory.

rglob

rglob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: bool = False) -> Iterator[Self]

Like :meth:glob, recursively.

mkdir

mkdir(mode: int = 511, parents: bool = False, exist_ok: bool = False) -> None

Create a directory at the path.

touch

touch(mode: int = 438, exist_ok: bool = True) -> None

Create the file at the path, or update its modification time.

unlink(*, missing_ok: bool = False) -> None

Remove the file at the path.

rmdir

rmdir(*, recursive: bool = False) -> None

Remove the directory at the path.

With recursive=True the whole tree is removed. The default is non-recursive and stays safe even on drivers whose own rmdir recurses by default (universal-pathlib).

copy

copy(target: Any, *, follow_symlinks: bool = True, preserve_metadata: bool = False) -> Self

Copy this file or directory to target; return the new path.

copy_into

copy_into(target_dir: Any, *, follow_symlinks: bool = True, preserve_metadata: bool = False) -> Self

Copy into target_dir, keeping this path's name.

move

move(target: Any) -> Self

Move this path to target; return the new path.

move_into

move_into(target_dir: Any) -> Self

Move into target_dir, keeping this path's name.

walk

walk(top_down: bool = True, on_error: Callable | None = None, follow_symlinks: bool = False) -> Iterator[tuple[Self, list[str], list[str]]]

Walk the tree, yielding (path, dirnames, filenames) per dir.

resolve

resolve(strict: bool = False) -> Self

The absolute path, with symlinks resolved.

absolute

absolute() -> Self

The absolute path, without resolving symlinks.

expanduser

expanduser() -> Self

The path with a leading ~ expanded.

readlink() -> Self

The path a symbolic link points to.

rename

rename(target: Any) -> Self

Rename the path to target and return the new path.

replace

replace(target: Any) -> Self

Rename the path to target, replacing any existing file.

chmod

chmod(mode: int, *, follow_symlinks: bool = True) -> None

Change the file mode and permission bits.

lchmod

lchmod(mode: int) -> None

Like :meth:chmod, without following symbolic links.

owner

owner(*, follow_symlinks: bool = True) -> str

The login name of the file's owner.

group

group(*, follow_symlinks: bool = True) -> str

The group name of the file.

symlink_to(target: Any, target_is_directory: bool = False) -> None

Make this path a symbolic link to target.

hardlink_to(target: Any) -> None

Make this path a hard link to target.

link_to(target: Any) -> None

Make target a hard link to this path.

.. deprecated:: link_to takes the reverse argument order of :meth:hardlink_to and was removed from pathlib in Python 3.12. Prefer :meth:hardlink_to; this is kept, and synthesized where the driver dropped it, only for backward compatibility.

as_url

as_url(**kwargs: Any) -> str

A URL for the path; keyword arguments pass to the driver.

download_to

download_to(destination: Any) -> Any

Download the path's contents to a local destination.

upload_from

upload_from(source: Any, **kwargs: Any) -> Any

Upload a local source to the path.

clear_cache

clear_cache() -> None

Discard any locally cached copy of the path (cloudpathlib).

rmtree

rmtree() -> None

Remove the directory tree at the path. Alias of rmdir.

copytree

copytree(target: Any, *, follow_symlinks: bool = True, preserve_metadata: bool = False) -> Self

Copy a directory tree to target. Alias of copy.