API Reference¶
Functions¶
This package offers the following functions:
- pathlib_abc.vfspath(obj)¶
Return the virtual filesystem path (a string) of the given object. Unlike the
os.fspath()function, this function calls the object’s__vfspath__()method.
- pathlib_abc.vfsopen(obj, mode='r' buffering=-1, encoding=None, errors=None, newline=None)¶
Open the given object and return a file object. Unlike the built-in
open()function, this function additionally calls the object’s__open_reader__(),__open_writer__()or__open_updater__()method, as appropriate for the given mode.
Protocols¶
This package offers the following protocols:
- class pathlib_abc.PathParser¶
Protocol for path parser objects, which split and join string paths.
Subclasses of
JoinablePathshould provide a path parser object as an attribute namedparser.Path parsers provide a subset of the
os.pathAPI. Python itself provides theposixpathandntpathmodules, which can be assigned toparserto implement path objects with POSIX or Windows syntax.- sep¶
Character used to separate path components.
- altsep¶
Alternative path separator character, or
None.
- split(path)¶
Split the path into a pair
(head, tail), where head is everything before the final path separator, and tail is everything after. Either part may be empty.Note
Trailing slashes are meaningful in
posixpathandntpath, soP('foo/').parentisP('foo'), and itsnameis the empty string.
- splitext(name)¶
Split the filename into a pair
(stem, ext), where ext is a file extension beginning with a dot and containing at most one dot, and stem is everything before the extension.
- normcase(path)¶
Return the path with its case normalized.
Note
This method is used to detect case sensitivity in
JoinablePath.full_match()andReadablePath.glob(), where it’s called with the string containing a mix of upper and lowercase letters. Case-sensitive filesystems should return the string unchanged, whereas case-insensitive filesystems should return the string with its case modified (e.g. withupper()orlower().)
- class pathlib_abc.PathInfo¶
Protocol for path information objects, which provide file type info.
Subclasses of
ReadablePathshould provide a path information object as an attribute namedinfo.- exists(*, follow_symlinks=True)¶
Return
Trueif the path is an existing file or directory, or any other kind of file; returnFalseif the path doesn’t exist.If follow_symlinks is
False, returnTruefor symlinks without checking if their targets exist.
- is_dir(*, follow_symlinks=True)¶
Return
Trueif the path is a directory, or a symbolic link pointing to a directory; returnFalseif the path is (or points to) any other kind of file, or if it doesn’t exist.If follow_symlinks is
False, returnTrueonly if the path is a directory (without following symlinks); returnFalseif the path is any other kind of file, or if it doesn’t exist.
- is_file(*, follow_symlinks=True)¶
Return
Trueif the path is a file, or a symbolic link pointing to a file; returnFalseif the path is (or points to) a directory or other non-file, or if it doesn’t exist.If follow_symlinks is
False, returnTrueonly if the path is a file (without following symlinks); returnFalseif the path is a directory or other other non-file, or if it doesn’t exist.
- is_symlink()¶
Return
Trueif the path is a symbolic link (even if broken); returnFalseif the path is a directory or any kind of file, or if it doesn’t exist.
Abstract base classes¶
This package offers the following abstract base classes:
ABC |
Inherits from |
Abstract methods |
Mixin methods |
|---|---|---|---|
|
|
|||
- class pathlib_abc.JoinablePath¶
Abstract base class for path objects without I/O support.
- parser¶
(Abstract attribute.) Implementation of
PathParserused for low-level splitting and joining.
- __vfspath__()¶
(Abstract method.) Return a string representation of the path, suitable for passing to methods of the
parser.
- with_segments(*pathsegments)¶
(Abstract method.) Create a new path object of the same type by combining the given pathsegments. This method is called whenever a derivative path is created, such as from
parentandwith_name().
- parts¶
Tuple of path components. The default implementation repeatedly calls
PathParser.split()to decompose the path.
- anchor¶
The path’s irreducible prefix. The default implementation repeatedly calls
PathParser.split()until the directory name stops changing.
- parent¶
The path’s lexical parent. The default implementation calls
PathParser.split()once.
- parents¶
Sequence of the path’s lexical parents, beginning with the immediate parent. The default implementation repeatedly calls
PathParser.split().
- name¶
The path’s base name. The name is empty if the path has only an anchor, or ends with a slash. The default implementation calls
PathParser.split()once.
- stem¶
The path’s base name with the file extension omitted. The default implementation calls
PathParser.splitext()onname.
- suffix¶
The path’s file extension. The default implementation calls
PathParser.splitext()onname.
- suffixes¶
Sequence of the path’s file extensions. The default implementation repeatedly calls
PathParser.splitext()onname.
- with_name(name)¶
Return a new path with a different
name. The name may be empty. The default implementation callsPathParser.split()to remove the old name, andwith_segments()to create the new path object.
- with_stem(stem)¶
Return a new path with a different
stem, similarly towith_name().
- with_suffix(suffix)¶
Return a new path with a different
suffix, similarly towith_name().
- joinpath(*pathsegments)¶
Return a new path with the given path segments joined onto the end. The default implementation calls
with_segments()with the combined segments.
- __truediv__(pathsegment)¶
Return a new path with the given path segment joined on the end.
- __rtruediv__(pathsegment)¶
Return a new path with the given path segment joined on the beginning.
- relative_to(other, *, walk_up=False)¶
Return a new relative path from other to this path. The default implementation compares this path and the parents of other;
__eq__()must be implemented for this to work correctly.
- is_relative_to(other)¶
Returns
Trueis this path is relative to other,Falseotherwise. The default implementation compares this path and the parents of other;__eq__()must be implemented for this to work correctly.
- full_match(pattern)¶
Return true if the path matches the given glob-style pattern, false otherwise. The default implementation uses
PathParser.normcase()to establish case sensitivity.
- class pathlib_abc.ReadablePath¶
Abstract base class for path objects with support for reading data. This is a subclass of
JoinablePath- __open_reader__()¶
(Abstract method.) Open the path for reading in binary mode, and return a file object.
- iterdir()¶
(Abstract method.) Yield path objects for the directory contents.
- readlink()¶
(Abstract method.) Return the symlink target as a new path object.
- read_text(encoding=None, errors=None, newline=None)¶
Return the text contents of the path. The default implementation calls
vfsopen().
- copy(target, **kwargs)¶
Copy the path to the given target, which should be an instance of
WritablePath. The default implementation callsWritablePath._copy_from(), passing along keyword arguments.
- copy_into(target_dir, **kwargs)¶
Copy the path into the given target directory, which should be an instance of
WritablePath. Seecopy().
- glob(pattern, *, recurse_symlinks=True)¶
Yield path objects in the file tree that match the given glob-style pattern. The default implementation uses
infoanditerdir().Warning
For performance reasons, the default value for recurse_symlinks is
Truein this base class, but for historical reasons, the default isFalseinpathlib.Path. Furthermore,Trueis the only acceptable value for recurse_symlinks in this base class.For maximum compatibility, users should supply
recurse_symlinks=Trueexplicitly when globbing recursively.
- class pathlib_abc.WritablePath¶
Abstract base class for path objects with support for writing data. This is a subclass of
JoinablePath- __open_writer__(mode)¶
(Abstract method.) Open the path for writing in binary mode, and return a file object. The mode argument is either
'w','a', or'x'.
- mkdir()¶
(Abstract method.) Create this path as a directory.
- symlink_to(target, target_is_directory=False)¶
(Abstract method.) Create this path as a symlink to the given target.
- write_bytes(data)¶
Write the given binary data to the path, and return the number of bytes written. The default implementation calls
vfsopen().
- write_text(data, encoding=None, errors=None, newline=None)¶
Write the given text data to the path, and return the number of bytes written. The default implementation calls
vfsopen().
- _copy_from(source, *, follow_symlinks=True)¶
Copy the path from the given source, which should be an instance of
ReadablePath. The default implementation usesReadablePath.infoto establish the type of the source path. It usesvfsopen()to copy regular files;iterdir()andmkdir()to copy directories; andreadlink()andsymlink_to()to copy symlinks when follow_symlinks is false.