On this page
pathlib.PosixPath
Prev: pathlib.Path | Up: pathlib | Next: pathlib.PurePath
Rel: pathlib.Path, pathlib.PurePath, pathlib.PurePosixPath, pathlib.PureWindowsPath
Ref: docs.python.org — pathlib.PosixPath
Tags: #public #python3 #stdlib #pathlib
Import
import pathlib
from pathlib import PosixPath
Signature & Overview
pathlib.PosixPath(*args, **kwargs)
Path subclass for non-Windows systems.
Methods & Attributes
PosixPath.absolute(self)— Return an absolute version of this path No normalization or symlink resolution is performed.PosixPath.anchor(...)— The concatenation of the drive and root, or ''.PosixPath.as_posix(self)— Return the string representation of the path with forward (/) slashes.PosixPath.as_uri(self)— Return the path as a URI.PosixPath.chmod(self, mode, *, follow_symlinks=True)— Change the permissions of the path, like os.chmod().PosixPath.copy(self, target, **kwargs)— Recursively copy this file or directory tree to the given destination.PosixPath.copy_into(self, target_dir, **kwargs)— Copy this file or directory tree into the given existing directory.PosixPath.cwd()— Return a new path pointing to the current working directory.PosixPath.drive(...)— The drive prefix (letter or UNC path), if any.PosixPath.exists(self, *, follow_symlinks=True)— Whether this path exists.PosixPath.expanduser(self)— Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser)PosixPath.from_uri(uri)— Return a new path from the given 'file' URI.PosixPath.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.PosixPath.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.PosixPath.group(self, *, follow_symlinks=True)— Return the group name of the file gid.PosixPath.hardlink_to(self, target)— Make this path a hard link pointing to the same file as target.PosixPath.home()— Return a new path pointing to expanduser('~').PosixPath.info(...)— A PathInfo object that exposes the file type and other file attributes of this path.PosixPath.is_absolute(self)— True if the path is absolute (has both a root and, if applicable, a drive).PosixPath.is_block_device(self)— Whether this path is a block device.PosixPath.is_char_device(self)— Whether this path is a character device.PosixPath.is_dir(self, *, follow_symlinks=True)— Whether this path is a directory.PosixPath.is_fifo(self)— Whether this path is a FIFO.PosixPath.is_file(self, *, follow_symlinks=True)— Whether this path is a regular file (also True for symlinks pointing to regular files).PosixPath.is_junction(self)— Whether this path is a junction.PosixPath.is_mount(self)— Check if this path is a mount pointPosixPath.is_relative_to(self, other)— Return True if the path is relative to another path or False.PosixPath.is_reserved(self)— Return True if the path contains one of the special names reserved by the system, if any.PosixPath.is_socket(self)— Whether this path is a socket.PosixPath.is_symlink(self)— Whether this path is a symbolic link.PosixPath.iterdir(self)— Yield path objects of the directory contents.PosixPath.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 isPosixPath.lchmod(self, mode)— Like chmod(), except if the path points to a symlink, the symlink's permissions are changed, rather than its target's.PosixPath.lstat(self)— Like stat(), except if the path points to a symlink, the symlink's status information is returned, rather than its target's.PosixPath.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 thisPosixPath.mkdir(self, mode=511, parents=False, exist_ok=False)— Create a new directory at this given path.PosixPath.move(self, target)— Recursively move this file or directory tree to the given destination.PosixPath.move_into(self, target_dir)— Move this file or directory tree into the given existing directory.PosixPath.name(...)— The final path component, if any.PosixPath.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
import os
from pathlib import PosixPath
if os.name == "posix":
p = PosixPath("/usr/local/bin/python3")
assert p.parent == PosixPath("/usr/local/bin")
assert p.parts == ("/", "usr", "local", "bin", "python3")
Gotchas
- Fails to instantiate on Windows: Calling
PosixPath(...)on Windows raisesUnsupportedOperation(orNotImplementedErroron older Python). - Prefer
PathorPurePosixPath: UsePathfor portable filesystem I/O andPurePosixPathwhen you only need POSIX path math on any OS.
REPL Inspect
from pathlib import PosixPath
help(PosixPath)
Linked from
- tuts/python3/stdlib_module/pathlib
- tuts/python3/stdlib_module/pathlib/Path
- 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/Path 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