Skip to content
module

panpath.base

Base class for all PanPath path implementations.

Classes
class

panpath.base.PanPath(*args, **kwargs)PanPath

Bases
pathlib.Path pathlib.PurePath

Universal path base class and factory.

This class inherits from pathlib.Path and serves dual purposes: 1. Base class for all path types in the panpath package 2. Factory for creating appropriate path instances via new

As a base class, it's inherited by:

  • - LocalPath (local filesystem paths with sync and async methods)
  • - CloudPath (cloud storage paths with sync and async methods)
  • - All cloud-specific subclasses (GSPath, S3Path, AzurePath, etc.)

As a factory, calling PanPath(...) returns the appropriate concrete implementationbased on the URI scheme.

Use isinstance(obj, PanPath) to check if an object is a path created by this package.

Attributes
  • anchor The concatenation of the drive and root, or ''.</>
  • drive The drive prefix (letter or UNC path), if any.</>
  • name The final path component, if any.</>
  • parent The logical parent of the path.</>
  • parents A sequence of this path's logical parents.</>
  • parts An object providing sequence-like access to thecomponents in the filesystem path. </>
  • root The root of the path, if any.</>
  • stem The final path component, minus its last suffix.</>
  • suffix The final component's last suffix, if any.
    This includes the leading period. For example: '.txt' </>
  • suffixes A list of the final component's suffixes, if any.
    These include the leading periods. For example: ['.tar', '.gz'] </>
Examples
>>> # Local path>>> path = PanPath("/local/file.txt")
>>> isinstance(path, PanPath)
True
>>> # S3 path
>>> path = PanPath("s3://bucket/key.txt")
>>> isinstance(path, PanPath)
True
>>> # Async method with a_ prefix
>>> content = await path.a_read_text()
Methods
  • __bytes__() Return the bytes representation of the path. This is onlyrecommended to use under Unix. </>
  • __new__(cls, *args, **kwargs) (PanPath) Create and return the appropriate path instance.</>
  • __str__() Return the string representation of the path, suitable forpassing to system calls. </>
  • a_copy(target) (PanPath) Asynchronously copy this path to the target path.</>
  • a_copytree(target, follow_symlinks) (PanPath) Asynchronously copy the directory and all its contents recursively to the target path.</>
  • a_exists() (bool) Asynchronously check if the path exists.</>
  • a_glob(pattern) (AsyncGenerator) Asynchronously yield paths matching a glob pattern.</>
  • a_is_dir() (bool) Asynchronously check if the path is a directory.</>
  • a_is_file() (bool) Asynchronously check if the path is a file.</>
  • a_is_symlink() (bool) Asynchronously check if the path is a symbolic link.</>
  • a_iterdir() (AsyncGenerator) Asynchronously iterate over directory contents.</>
  • a_mkdir(mode, parents, exist_ok) Asynchronously create a directory at this path.</>
  • a_open(mode, encoding, **kwargs) (AsyncFileHandle) Asynchronously open the file and return an async file handle.</>
  • a_read_bytes() (bytes) Asynchronously read the file's bytes.</>
  • a_read_text(encoding) (str) Asynchronously read the file's text content.</>
  • a_readlink() (PanPath) Asynchronously read the target of the symbolic link.</>
  • a_rename(target) (PanPath) Asynchronously rename this path to the target path.</>
  • a_replace(target) (PanPath) Asynchronously replace this path with the target path.</>
  • a_resolve() (PanPath) Resolve to absolute path (no-op for cloud paths).</>
  • a_rglob(pattern) (AsyncGenerator) Asynchronously yield paths matching a recursive glob pattern.</>
  • a_rmdir() Asynchronously remove the directory and its contents recursively.</>
  • a_rmtree(ignore_errors, onerror) Asynchronously remove the directory and all its contents recursively.</>
  • a_stat(follow_symlinks) (stat_result) Asynchronously get the file or directory's status information.</>
  • a_symlink_to(target, target_is_directory) Asynchronously create a symbolic link pointing to the target path.</>
  • a_touch(mode, exist_ok) Asynchronously create the file if it does not exist.</>
  • a_unlink(missing_ok) Asynchronously remove (delete) the file or empty directory.</>
  • a_walk() (AsyncGenerator) Asynchronously walk the directory tree.</>
  • a_write_bytes(data) (Optional) Asynchronously write bytes to the file.</>
  • a_write_text(data, encoding) (int) Asynchronously write text to the file.</>
  • absolute() Return an absolute version of this path by prepending the currentworking directory. No normalization or symlink resolution is performed. </>
  • as_posix() Return the string representation of the path with forward (/)slashes. </>
  • as_uri() Return the path as a 'file' URI.</>
  • chmod(mode, follow_symlinks) Change the permissions of the path, like os.chmod().</>
  • cwd() Return a new path pointing to the current working directory.</>
  • exists(follow_symlinks) Whether this path exists.</>
  • expanduser() Return a new path with expanded ~ and ~user constructs(as returned by os.path.expanduser) </>
  • glob(pattern, case_sensitive) Iterate over this subtree and yield all existing files (of anykind, including directories) matching the given relative pattern. </>
  • group() Return the group name of the file gid.</>
  • hardlink_to(target) Make this path a hard link pointing to the same file as target.</>
  • home() Return a new path pointing to the user's home directory (asreturned by os.path.expanduser('~')). </>
  • is_absolute() True if the path is absolute (has both a root and, if applicable,a drive). </>
  • is_block_device() Whether this path is a block device.</>
  • is_char_device() Whether this path is a character device.</>
  • is_dir() Whether this path is a directory.</>
  • is_fifo() Whether this path is a FIFO.</>
  • is_file() Whether this path is a regular file (also True for symlinks pointingto regular files). </>
  • is_junction() Whether this path is a junction.</>
  • is_mount() Check if this path is a mount point</>
  • is_relative_to(other, *_deprecated) Return True if the path is relative to another path or False.</>
  • is_reserved() Return True if the path contains one of the special names reservedby the system, if any. </>
  • is_socket() Whether this path is a socket.</>
  • is_symlink() Whether this path is a symbolic link.</>
  • iterdir() Yield path objects of the directory contents.</>
  • joinpath(*pathsegments) Combine this path with one or several arguments, and return anew path representing either a subpath (if all arguments are relative paths) or a totally different path (if one of the arguments is anchored). </>
  • lchmod(mode) Like chmod(), except if the path points to a symlink, the symlink'spermissions are changed, rather than its target's. </>
  • lstat() Like stat(), except if the path points to a symlink, the symlink'sstatus information is returned, rather than its target's. </>
  • match(path_pattern, case_sensitive) Return True if this path matches the given pattern.</>
  • mkdir(mode, parents, exist_ok) Create a new directory at this given path.</>
  • open(mode, buffering, encoding, errors, newline) Open the file pointed to by this path and return a file object, asthe built-in open() function does. </>
  • owner() Return the login name of the file owner.</>
  • read_bytes() Open the file in bytes mode, read it, and close the file.</>
  • read_text(encoding, errors) Open the file in text mode, read it, and close the file.</>
  • readlink() Return the path to which the symbolic link points.</>
  • relative_to(other, *_deprecated, walk_up) Return the relative path to another path identified by the passedarguments. If the operation is not possible (because this is not related to the other path), raise ValueError. </>
  • rename(target) Rename this path to the target path.</>
  • replace(target) Rename this path to the target path, overwriting if that path exists.</>
  • resolve(strict) Make the path absolute, resolving all symlinks on the way and alsonormalizing it. </>
  • rglob(pattern, case_sensitive) Recursively yield all existing files (of any kind, includingdirectories) matching the given relative pattern, anywhere in this subtree. </>
  • rmdir() Remove this directory. The directory must be empty.</>
  • samefile(other_path) Return whether other_path is the same or not as this file(as returned by os.path.samefile()). </>
  • stat(follow_symlinks) Return the result of the stat() system call on this path, likeos.stat() does. </>
  • symlink_to(target, target_is_directory) Make this path a symlink pointing to the target path.Note the order of arguments (link, target) is the reverse of os.symlink. </>
  • touch(mode, exist_ok) Create this file with the given access mode, if it doesn't exist.</>
  • unlink(missing_ok) Remove this file or link.If the path is a directory, use rmdir() instead. </>
  • walk() Walk the directory tree.</>
  • with_name(name) Return a new path with the file name changed.</>
  • with_segments(*pathsegments) Construct a new path object from any number of path-like objects.Subclasses may override this method to customize how new path objects are created from methods like iterdir(). </>
  • with_stem(stem) Return a new path with the stem changed.</>
  • with_suffix(suffix) Return a new path with the file suffix changed. If the pathhas no suffix, add given suffix. If the given suffix is an empty string, remove the suffix from the path. </>
  • write_bytes(data) Open the file in bytes mode, write to it, and close the file.</>
  • write_text(data, encoding, errors, newline) Open the file in text mode, write to it, and close the file.</>
method

with_segments(*pathsegments)

Construct a new path object from any number of path-like objects.Subclasses may override this method to customize how new path objects are created from methods like iterdir().

method

__str__()

Return the string representation of the path, suitable forpassing to system calls.

method

as_posix()

Return the string representation of the path with forward (/)slashes.

method

__bytes__()

Return the bytes representation of the path. This is onlyrecommended to use under Unix.

method

as_uri()

Return the path as a 'file' URI.

method

with_name(name)

Return a new path with the file name changed.

method

with_stem(stem)

Return a new path with the stem changed.

method

with_suffix(suffix)

Return a new path with the file suffix changed. If the pathhas no suffix, add given suffix. If the given suffix is an empty string, remove the suffix from the path.

method

relative_to(other, *_deprecated, walk_up=False)

Return the relative path to another path identified by the passedarguments. If the operation is not possible (because this is not related to the other path), raise ValueError.

The walk_up parameter controls whether .. may be used to resolve the path.

method

is_relative_to(other, *_deprecated)

Return True if the path is relative to another path or False.

method

joinpath(*pathsegments)

Combine this path with one or several arguments, and return anew path representing either a subpath (if all arguments are relative paths) or a totally different path (if one of the arguments is anchored).

method

is_absolute()

True if the path is absolute (has both a root and, if applicable,a drive).

method

is_reserved()

Return True if the path contains one of the special names reservedby the system, if any.

method

match(path_pattern, case_sensitive=None)

Return True if this path matches the given pattern.

method

stat(follow_symlinks=True)

Return the result of the stat() system call on this path, likeos.stat() does.

method

lstat()

Like stat(), except if the path points to a symlink, the symlink'sstatus information is returned, rather than its target's.

method

exists(follow_symlinks=True)

Whether this path exists.

This method normally follows symlinks; to check whether a symlink exists, add the argument follow_symlinks=False.

method

is_dir()

Whether this path is a directory.

method

is_file()

Whether this path is a regular file (also True for symlinks pointingto regular files).

method

is_mount()

Check if this path is a mount point

method

is_junction()

Whether this path is a junction.

method

is_block_device()

Whether this path is a block device.

method

is_char_device()

Whether this path is a character device.

method

is_fifo()

Whether this path is a FIFO.

method

is_socket()

Whether this path is a socket.

method

samefile(other_path)

Return whether other_path is the same or not as this file(as returned by os.path.samefile()).

method

open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)

Open the file pointed to by this path and return a file object, asthe built-in open() function does.

method

read_bytes()

Open the file in bytes mode, read it, and close the file.

method

read_text(encoding=None, errors=None)

Open the file in text mode, read it, and close the file.

method

write_bytes(data)

Open the file in bytes mode, write to it, and close the file.

method

write_text(data, encoding=None, errors=None, newline=None)

Open the file in text mode, write to it, and close the file.

generator

iterdir()

Yield path objects of the directory contents.

The children are yielded in arbitrary order, and the special entries '.' and '..' are not included.

generator

glob(pattern, case_sensitive=None)

Iterate over this subtree and yield all existing files (of anykind, including directories) matching the given relative pattern.

generator

rglob(pattern, case_sensitive=None)

Recursively yield all existing files (of any kind, includingdirectories) matching the given relative pattern, anywhere in this subtree.

classmethod

cwd()

Return a new path pointing to the current working directory.

classmethod

home()

Return a new path pointing to the user's home directory (asreturned by os.path.expanduser('~')).

method

absolute()

Return an absolute version of this path by prepending the currentworking directory. No normalization or symlink resolution is performed.

Use resolve() to get the canonical path to a file.

method

resolve(strict=False)

Make the path absolute, resolving all symlinks on the way and alsonormalizing it.

method

owner()

Return the login name of the file owner.

method

group()

Return the group name of the file gid.

method

touch(mode=438, exist_ok=True)

Create this file with the given access mode, if it doesn't exist.

method

mkdir(mode=511, parents=False, exist_ok=False)

Create a new directory at this given path.

method

chmod(mode, follow_symlinks=True)

Change the permissions of the path, like os.chmod().

method

lchmod(mode)

Like chmod(), except if the path points to a symlink, the symlink'spermissions are changed, rather than its target's.

method

rmdir()

Remove this directory. The directory must be empty.

method

rename(target)

Rename this path to the target path.

The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.

Returns the new Path instance pointing to the target path.

method

replace(target)

Rename this path to the target path, overwriting if that path exists.

The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.

Returns the new Path instance pointing to the target path.

method

expanduser()

Return a new path with expanded ~ and ~user constructs(as returned by os.path.expanduser)

staticmethod

__new__(cls, *args, **kwargs)PanPath

Create and return the appropriate path instance.

If called on a subclass, returns instance of that subclass. If called on PanPath itself, routes to the appropriate concrete class.

method

a_resolve()

Resolve to absolute path (no-op for cloud paths).

Returns (PanPath)

Self (cloud paths are already absolute)

method

a_exists()

Asynchronously check if the path exists.

Returns (bool)

True if the path exists, False otherwise.

method

a_read_bytes()

Asynchronously read the file's bytes.

Returns (bytes)

File content as bytes.

method

a_read_text(encoding='utf-8')

Asynchronously read the file's text content.

Parameters
  • encoding (str, optional) Text encoding to use (default: 'utf-8')
Returns (str)

File content as string.

method

a_write_bytes(data)

Asynchronously write bytes to the file.

Parameters
  • data (bytes) Bytes to write to the file.
Returns (Optional)

Number of bytes written. For some cloud paths, may return None.

method

a_write_text(data, encoding='utf-8')

Asynchronously write text to the file.

Parameters
  • data (str) Text to write to the file.
  • encoding (str, optional) Text encoding to use (default: 'utf-8')
Returns (int)

Number of characters written.

method

a_iterdir()

Asynchronously iterate over directory contents.

Yields (AsyncGenerator)

PanPath instances for each item in the directory.

method

a_is_dir()

Asynchronously check if the path is a directory.

Returns (bool)

True if the path is a directory, False otherwise.

method

a_is_file()

Asynchronously check if the path is a file.

Returns (bool)

True if the path is a file, False otherwise.

method

a_stat(follow_symlinks=True)

Asynchronously get the file or directory's status information.

Returns (stat_result)

An object containing file status information (platform-dependent).

method

a_mkdir(mode=511, parents=False, exist_ok=False)

Asynchronously create a directory at this path.

Parameters
  • mode (int, optional) Directory mode (permissions) to set.
  • parents (bool, optional) If True, create parent directories as needed.
  • exist_ok (bool, optional) If True, does not raise an error if the directory already exists.
method

a_glob(pattern)

Asynchronously yield paths matching a glob pattern.

Parameters
  • pattern (str) Glob pattern to match.
Returns (AsyncGenerator)

List of PanPath instances matching the pattern.

method

a_rglob(pattern)

Asynchronously yield paths matching a recursive glob pattern.

Parameters
  • pattern (str) Recursive glob pattern to match.
Returns (AsyncGenerator)

List of PanPath instances matching the pattern.

method

a_walk()

Asynchronously walk the directory tree.

Yields (AsyncGenerator)

Tuples of (current_path, dirnames, filenames) at each level.

method

a_touch(mode=438, exist_ok=True)

Asynchronously create the file if it does not exist.

Parameters
  • mode (int, optional) File mode (permissions) to set if creating the file.
  • exist_ok (bool, optional) If False, raises an error if the file already exists.
method

a_rename(target)

Asynchronously rename this path to the target path.

Parameters
  • target (Union) New path to rename to.
Returns (PanPath)

The renamed PanPath instance.

method

a_replace(target)

Asynchronously replace this path with the target path.

Parameters
  • target (Union) New path to replace with.
Returns (PanPath)

The replaced PanPath instance.

method

a_rmdir()

Asynchronously remove the directory and its contents recursively.

method

a_rmtree(ignore_errors=False, onerror=None)

Asynchronously remove the directory and all its contents recursively.

Parameters
  • ignore_errors (bool, optional) If True, ignores errors during removal.
  • onerror (Any, optional) Optional function to call on errors.
method

a_copy(target)

Asynchronously copy this path to the target path.

Parameters
  • target (Union) Destination PanPath to copy to.
Returns (PanPath)

The copied PanPath instance.

method

a_copytree(target, follow_symlinks=True)

Asynchronously copy the directory and all its contents recursively to the target path.

Parameters
  • target (Union) Destination PanPath to copy to.
  • follow_symlinks (bool, optional) If True, copies the contents of symlinks.
Returns (PanPath)

The copied PanPath instance.

method

a_open(mode='r', encoding='utf-8', **kwargs)

Asynchronously open the file and return an async file handle.

Parameters
  • mode (str, optional) Mode to open the file (e.g., 'r', 'rb', 'w', 'wb').
  • encoding (str, optional) Text encoding to use (default: 'utf-8').
  • **kwargs (Any) Additional arguments to pass to the underlying open method.
Returns (AsyncFileHandle)

An async file handle.

method

walk()

Walk the directory tree.

Yields

Tuples of (current_path, dirnames, filenames) at each level.