On this page
pathlib.Path
Up: pathlib | Next: pathlib.PosixPath
Rel: pathlib.PosixPath, pathlib.PurePath, pathlib.PurePosixPath, pathlib.PureWindowsPath
Ref: docs.python.org — pathlib.Path
Tags: #public #python3 #stdlib #pathlib
Import
import pathlib
from pathlib import Path
Signature & Overview
pathlib.Path(*args, **kwargs)
PurePath subclass that can make system calls.
Methods & Attributes
Path.absolute(self)— Return an absolute version of this path No normalization or symlink resolution is performed.Path.anchor(...)— The concatenation of the drive and root, or ''.Path.as_posix(self)— Return the string representation of the path with forward (/) slashes.Path.as_uri(self)— Return the path as a URI.Path.chmod(self, mode, *, follow_symlinks=True)— Change the permissions of the path, like os.chmod().Path.copy(self, target, **kwargs)— Recursively copy this file or directory tree to the given destination.Path.copy_into(self, target_dir, **kwargs)— Copy this file or directory tree into the given existing directory.Path.cwd()— Return a new path pointing to the current working directory.Path.drive(...)— The drive prefix (letter or UNC path), if any.Path.exists(self, *, follow_symlinks=True)— Whether this path exists.Path.expanduser(self)— Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser)Path.from_uri(uri)— Return a new path from the given 'file' URI.Path.full_match(self, pattern, *, case_sensitive=None)— Return True if this path matches the given glob-style pattern. The pattern is matched against the entire path.Path.glob(self, pattern, *, case_sensitive=None, recurse_symlinks=False)— Iterate over this subtree and yield all existing files (of any kind, including directories) matching the given relative pattern.Path.group(self, *, follow_symlinks=True)— Return the group name of the file gid.Path.hardlink_to(self, target)— Make this path a hard link pointing to the same file as target.Path.home()— Return a new path pointing to expanduser('~').Path.info(...)— A PathInfo object that exposes the file type and other file attributes of this path.Path.is_absolute(self)— True if the path is absolute (has both a root and, if applicable, a drive).Path.is_block_device(self)— Whether this path is a block device.Path.is_char_device(self)— Whether this path is a character device.Path.is_dir(self, *, follow_symlinks=True)— Whether this path is a directory.Path.is_fifo(self)— Whether this path is a FIFO.Path.is_file(self, *, follow_symlinks=True)— Whether this path is a regular file (also True for symlinks pointing to regular files).Path.is_junction(self)— Whether this path is a junction.Path.is_mount(self)— Check if this path is a mount pointPath.is_relative_to(self, other)— Return True if the path is relative to another path or False.Path.is_reserved(self)— Return True if the path contains one of the special names reserved by the system, if any.Path.is_socket(self)— Whether this path is a socket.Path.is_symlink(self)— Whether this path is a symbolic link.Path.iterdir(self)— Yield path objects of the directory contents.Path.joinpath(self, *pathsegments)— 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 isPath.lchmod(self, mode)— Like chmod(), except if the path points to a symlink, the symlink's permissions are changed, rather than its target's.Path.lstat(self)— Like stat(), except if the path points to a symlink, the symlink's status information is returned, rather than its target's.Path.match(self, path_pattern, *, case_sensitive=None)— Return True if this path matches the given pattern. If the pattern is relative, matching is done from the right; otherwise, the entire path is matched. The recursive wildcard '**' is not supported by thisPath.mkdir(self, mode=511, parents=False, exist_ok=False)— Create a new directory at this given path.Path.move(self, target)— Recursively move this file or directory tree to the given destination.Path.move_into(self, target_dir)— Move this file or directory tree into the given existing directory.Path.name(...)— The final path component, if any.Path.open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None)— Open the file pointed to by this path and return a file object, as the built-in open() function does.
Usage
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "notes" / "todo.txt"
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text("ship docs\n", encoding="utf-8")
assert p.exists() and p.is_file()
assert p.name == "todo.txt" and p.stem == "todo" and p.suffix == ".txt"
assert p.read_text(encoding="utf-8").strip() == "ship docs"
Gotchas
- Platform-dispatched concrete path:
Path(...)instantiatesPosixPathon macOS/Linux andWindowsPathon Windows. - Right-hand absolute path overrides the left:
Path("/a/b") / "/c"evaluates toPath("/c"), notPath("/a/b/c"). - Always specify
encoding="utf-8"onread_text()andwrite_text()so behavior doesn't vary by OS locale.
REPL Inspect
from pathlib import Path
help(Path)
Linked from
- tuts/python3/stdlib_module/pathlib
- tuts/python3/stdlib_module/pathlib/PosixPath
- tuts/python3/stdlib_module/pathlib/PurePath
- tuts/python3/stdlib_module/pathlib/PurePosixPath
- tuts/python3/stdlib_module/pathlib/PureWindowsPath
- tuts/python3/stdlib_module/pathlib/UnsupportedOperation
- tuts/python3/stdlib_module/pathlib/WindowsPath
Related notes
- tuts/python3/stdlib_module/pathlib/PosixPath Linked here; links to this page; shared tags: #pathlib, #stdlib
- tuts/python3/stdlib_module/pathlib/PurePath Linked here; links to this page; shared tags: #pathlib, #stdlib
- tuts/python3/stdlib_module/pathlib/PurePosixPath Linked here; links to this page; shared tags: #pathlib, #stdlib
- tuts/python3/stdlib_module/pathlib/PureWindowsPath Linked here; links to this page; shared tags: #pathlib, #stdlib
- tuts/python3/stdlib_module/pathlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib/UnsupportedOperation links to this page; shared tags: #pathlib, #stdlib
- tuts/python3/stdlib_module/pathlib/WindowsPath links to this page; shared tags: #pathlib, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib