pathlib.Path

The snippet below shows how autoclasstoc can be used to document the pathlib.Path class from the Python standard library. This example also includes inheritance. Note that inherited methods are collapsed in the TOC by default, but can easily be expanded.

.. autoclass:: pathlib.Path
  :members:
  :private-members:
  :special-members:
  :inherited-members:
  :undoc-members:

  .. autoclasstoc::
class pathlib.Path(*args, **kwargs)[source]

PurePath subclass that can make system calls.

Path represents a filesystem path but unlike PurePath, also offers methods to do system calls on path objects. Depending on your system, instantiating a Path will return either a PosixPath or a WindowsPath object. You can also instantiate a PosixPath or WindowsPath directly, but cannot instantiate a WindowsPath on a POSIX system or vice versa.

Public Data Attributes:

Inherited from PurePath

drive

The drive prefix (letter or UNC path), if any.

root

The root of the path, if any.

anchor

The concatenation of the drive and root, or ‘’.

name

The final path component, if any.

suffix

The final component’s last suffix, if any.

suffixes

A list of the final component’s suffixes, if any.

stem

The final path component, minus its last suffix.

parts

An object providing sequence-like access to the components in the filesystem path.

parent

The logical parent of the path.

parents

A sequence of this path’s logical parents.

Public Methods:

__new__(cls, *args, **kwargs)

Construct a PurePath from one or several strings and or existing PurePath objects.

__enter__()

__exit__(t, v, tb)

cwd()

Return a new path pointing to the current working directory (as returned by os.getcwd()).

home()

Return a new path pointing to the user’s home directory (as returned by os.path.expanduser(‘~’)).

samefile(other_path)

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

iterdir()

Iterate over the files in this directory.

glob(pattern)

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

rglob(pattern)

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

absolute()

Return an absolute version of this path.

resolve([strict])

Make the path absolute, resolving all symlinks on the way and also normalizing it (for example turning slashes into backslashes under Windows).

stat()

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

owner()

Return the login name of the file owner.

group()

Return the group name of the file gid.

open([mode, buffering, encoding, errors, …])

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

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.

write_bytes(data)

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

write_text(data[, encoding, errors])

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

touch([mode, exist_ok])

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

mkdir([mode, parents, exist_ok])

Create a new directory at this given path.

chmod(mode)

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

lchmod(mode)

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

unlink()

Remove this file or link.

rmdir()

Remove this directory.

lstat()

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

rename(target)

Rename this path to the given path.

replace(target)

Rename this path to the given path, clobbering the existing destination if it exists.

symlink_to(target[, target_is_directory])

Make this path a symlink pointing to the given path.

exists()

Whether this path exists.

is_dir()

Whether this path is a directory.

is_file()

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

is_symlink()

Whether this path is a symbolic link.

is_block_device()

Whether this path is a block device.

is_char_device()

Whether this path is a character device.

is_fifo()

Whether this path is a FIFO.

is_socket()

Whether this path is a socket.

expanduser()

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

Inherited from PurePath

__new__(cls, *args, **kwargs)

Construct a PurePath from one or several strings and or existing PurePath objects.

__reduce__()

helper for pickle

__str__()

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

__fspath__()

as_posix()

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

__bytes__()

Return the bytes representation of the path.

__repr__()

Return repr(self).

as_uri()

Return the path as a ‘file’ URI.

__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__lt__(other)

Return self<value.

__le__(other)

Return self<=value.

__gt__(other)

Return self>value.

__ge__(other)

Return self>=value.

with_name(name)

Return a new path with the file name changed.

with_suffix(suffix)

Return a new path with the file suffix changed.

relative_to(*other)

Return the relative path to another path identified by the passed arguments.

joinpath(*args)

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

__truediv__(key)

__rtruediv__(key)

is_absolute()

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

is_reserved()

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

match(path_pattern)

Return True if this path matches the given pattern.

Private Data Attributes:

_accessor

_closed

Inherited from PurePath

_cparts

_cached_cparts

_drv

_hash

_parts

_pparts

_root

_str

Private Methods:

_init([template])

_make_child_relpath(part)

_raise_closed()

_opener(name, flags[, mode])

_raw_open(flags[, mode])

Open the file pointed by this path and return a file descriptor, as os.open() does.

Inherited from PurePath

_parse_args(args)

_from_parts(args[, init])

_from_parsed_parts(drv, root, parts[, init])

_format_parsed_parts(drv, root, parts)

_init([template])

_make_child(args)


__bytes__()

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

__enter__()[source]
__eq__(other)

Return self==value.

__exit__(t, v, tb)[source]
__fspath__()
__ge__(other)

Return self>=value.

__gt__(other)

Return self>value.

__hash__()

Return hash(self).

__le__(other)

Return self<=value.

__lt__(other)

Return self<value.

static __new__(cls, *args, **kwargs)[source]

Construct a PurePath from one or several strings and or existing PurePath objects. The strings and path objects are combined so as to yield a canonicalized path, which is incorporated into the new PurePath object.

__reduce__()

helper for pickle

__repr__()

Return repr(self).

__rtruediv__(key)
__slots__ = ('_accessor', '_closed')
__str__()

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

__truediv__(key)
_accessor
_cached_cparts
_closed
property _cparts
_drv
classmethod _format_parsed_parts(drv, root, parts)
classmethod _from_parsed_parts(drv, root, parts, init=True)
classmethod _from_parts(args, init=True)
_hash
_init(template=None)[source]
_make_child(args)
_make_child_relpath(part)[source]
_opener(name, flags, mode=438)[source]
classmethod _parse_args(args)
_parts
_pparts
_raise_closed()[source]
_raw_open(flags, mode=511)[source]

Open the file pointed by this path and return a file descriptor, as os.open() does.

_root
_str
absolute()[source]

Return an absolute version of this path. This function works even if the path doesn’t point to anything.

No normalization is done, i.e. all ‘.’ and ‘..’ will be kept along. Use resolve() to get the canonical path to a file.

property anchor

The concatenation of the drive and root, or ‘’.

as_posix()

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

as_uri()

Return the path as a ‘file’ URI.

chmod(mode)[source]

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

classmethod cwd()[source]

Return a new path pointing to the current working directory (as returned by os.getcwd()).

property drive

The drive prefix (letter or UNC path), if any.

exists()[source]

Whether this path exists.

expanduser()[source]

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

glob(pattern)[source]

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

group()[source]

Return the group name of the file gid.

classmethod home()[source]

Return a new path pointing to the user’s home directory (as returned by os.path.expanduser(‘~’)).

is_absolute()

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

is_block_device()[source]

Whether this path is a block device.

is_char_device()[source]

Whether this path is a character device.

is_dir()[source]

Whether this path is a directory.

is_fifo()[source]

Whether this path is a FIFO.

is_file()[source]

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

is_reserved()

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

is_socket()[source]

Whether this path is a socket.

Whether this path is a symbolic link.

iterdir()[source]

Iterate over the files in this directory. Does not yield any result for the special paths ‘.’ and ‘..’.

joinpath(*args)

Combine this path with one or several arguments, and return a new 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)[source]

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

lstat()[source]

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

match(path_pattern)

Return True if this path matches the given pattern.

mkdir(mode=511, parents=False, exist_ok=False)[source]

Create a new directory at this given path.

property name

The final path component, if any.

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

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

owner()[source]

Return the login name of the file owner.

property parent

The logical parent of the path.

property parents

A sequence of this path’s logical parents.

property parts

An object providing sequence-like access to the components in the filesystem path.

read_bytes()[source]

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

read_text(encoding=None, errors=None)[source]

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

relative_to(*other)

Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not a subpath of the other path), raise ValueError.

rename(target)[source]

Rename this path to the given path.

replace(target)[source]

Rename this path to the given path, clobbering the existing destination if it exists.

resolve(strict=False)[source]

Make the path absolute, resolving all symlinks on the way and also normalizing it (for example turning slashes into backslashes under Windows).

rglob(pattern)[source]

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

rmdir()[source]

Remove this directory. The directory must be empty.

property root

The root of the path, if any.

samefile(other_path)[source]

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

stat()[source]

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

property stem

The final path component, minus its last suffix.

property suffix

The final component’s last suffix, if any.

property suffixes

A list of the final component’s suffixes, if any.

Make this path a symlink pointing to the given path. Note the order of arguments (self, target) is the reverse of os.symlink’s.

touch(mode=438, exist_ok=True)[source]

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

Remove this file or link. If the path is a directory, use rmdir() instead.

with_name(name)

Return a new path with the file name changed.

with_suffix(suffix)

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

write_bytes(data)[source]

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

write_text(data, encoding=None, errors=None)[source]

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