bagof.paths.AsyncPath
Bases: PurePathMixin, LocalCopyMixin, BaseWrapper
The await version of Path: the same
methods, as coroutines.
The parts that only describe a path (name, parent, /) stay
plain; the parts that touch storage are awaited.
>>> import asyncio
>>> from bagof.paths import AsyncPath
>>> async def main() -> None:
... p = AsyncPath("/etc/hostname")
... if await p.exists():
... print(await p.read_text())
Methods:
exists
async
Whether the path exists.
On a local path this follows Python 3.14 on every version: an
error returns False rather than raising. A name too long for the
file system is not there, and neither is one whose parent you may
not search. To see the error, call stat(), which still raises.
The other status checks (is_file, is_dir, is_symlink, ...)
do the same.
follow_symlinks=False (also on is_file and is_dir) asks about
a symbolic link itself, on every Python version.
is_file
async
Whether the path is a regular file.
Like exists, it returns False on an error on a local path.
is_dir
async
Whether the path is a directory.
Like exists, it returns False on an error on a local path.
samefile
async
Whether the path and other refer to the same file.
strict=False works as in
Path.samefile.
is_junction
async
is_junction() -> bool
Whether the path is a junction (a Windows concept; else False).
open
async
open(mode: str = 'r', buffering: int = -1, encoding: str | None = None, errors: str | None = None, newline: str | None = None, *, atomic: bool = False) -> Any
Open the path and return an async file object.
Use it with async with / async for. The handle's reads and
writes are awaited directly on a native driver, or run in a worker
thread on a synchronous one. atomic=True works as in
Path.open: closing commits, leaving an
async with block with an error discards.
read_text
async
read_text(encoding: str | None = None, errors: str | None = None, newline: str | None = None) -> str
Read the whole file as text.
write_bytes
async
Write data to the file as bytes, replacing any content.
atomic=True works as in
Path.write_bytes.
write_text
async
write_text(data: str, encoding: str | None = None, errors: str | None = None, newline: str | None = None, *, atomic: bool = False) -> int
Write data to the file as text, replacing any content.
atomic=True works as in
Path.write_bytes.
staged
staged(*, copy: bool = False, replace_type: bool = False, keep_on_failure: bool = False) -> AsyncStage
A staging area for a new version of this path.
The same as Path.staged, used with
async with; the staged path is an AsyncPath:
Outside an async with block, await it to make the staging
area, then await its commit() or discard().
glob
async
glob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: bool = False) -> AsyncIterator[Self]
Yield the paths matching pattern under this directory.
rglob
async
rglob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: bool = False) -> AsyncIterator[Self]
Like glob, recursively.
mkdir
async
Create a directory at the path.
touch
async
Create the file at the path, or update its modification time.
copy
async
Copy this file or directory to target; return the new path.
copy_into
async
copy_into(target_dir: Any, *, follow_symlinks: bool = True, preserve_metadata: bool = False) -> Self
Copy into target_dir, keeping this path's name.
walk
async
walk(top_down: bool = True, on_error: Callable | None = None, follow_symlinks: bool = False) -> AsyncIterator[tuple[Self, list[str], list[str]]]
Walk the tree, yielding (path, dirnames, filenames) per dir.
One directory per thread hop, so mutating dirnames in place to
prune the descent works exactly as it does on the sync wrapper. Note
that on_error runs in a worker thread.
A natively-async driver walks on its own coroutine surface; pruning
by editing dirnames is not propagated there, since the driver
produced the whole level before it was yielded.
chmod
async
Change the file mode and permission bits.
symlink_to
async
Make this path a symbolic link to target.
link_to
async
link_to(target: Any) -> None
Make target a hard link to this path.
.. deprecated::
link_to takes the reverse argument order of
hardlink_to and was removed from pathlib in Python
3.12. Prefer hardlink_to; this is kept, and synthesized
where the driver dropped it, only for backward compatibility.
download_to
async
Download the path's contents to a local destination.
bagof.paths.AsyncStage
AsyncStage(target: Any, *, copy: bool = False, replace_type: bool = False, keep_on_failure: bool = False)
Bases: _StageBase
The asynchronous staging area returned by
AsyncPath.staged.
Use it with async with, or await it to prepare it and then
await its commit or discard. The staging directory is made when
it is entered or awaited.
Attributes
path
property
path: Any
Where to create the new version: named like the target.
Available once the stage is entered or awaited.
Methods:
commit
async
Move what was created at path onto the target.
The same rules as Stage.commit.