pathlib
| Use Case | Command | Description |
|---|---|---|
| 📂 Create a path | Path('some/path') | Instantiate a path object (PosixPath or WindowsPath) |
| 🏠 User home dir | Path.home() | Path to user's home directory |
| 📁 Current working dir | Path.cwd() | Path to current working directory |
| 🔗 Join paths | path / 'subdir' / 'file.txt' | Combine with / operator |
| 👆 Parent directory | path.parent | Logical parent of the path |
| 📝 Read text | path.read_text() | Open file in text mode, read, and close |
| 📝 Write text | path.write_text('hello') | Open file in text mode, write, and close |
| 📝 Read bytes | path.read_bytes() | Read file as bytes |
| 📝 Write bytes | path.write_bytes(data) | Write bytes to file |
| ✅ Exists? | path.exists() | Check whether path exists |
| 📄 Is file? | path.is_file() | Check if regular file |
| 📂 Is directory? | path.is_dir() | Check if directory |
| 🔗 Is symlink? | path.is_symlink() | Check if symbolic link |
| 📋 List directory | path.iterdir() | Yield directory contents |
| 🔍 Glob pattern | path.glob('*.txt') | Yield files matching pattern |
| 🔍 Recursive glob | path.rglob('*.py') | Recursively yield matching files |
| 📛 Suffix | path.suffix | Last file extension (e.g. '.txt') |
| 📛 Stem | path.stem | Filename without suffix |
| 📛 Name | path.name | Final path component |
| 📌 Resolve | path.resolve() | Absolute path, resolve symlinks |
| 📌 Absolute | path.absolute() | Absolute version (no normalization) |
| 📌 Rename | path.rename('new') | Rename file/directory |
| 📌 Replace | path.replace('new') | Rename, overwriting if exists |
| 🗑️ Unlink | path.unlink() | Remove file or link |
| 🗑️ Rmdir | path.rmdir() | Remove empty directory |
| 📁 Mkdir | path.mkdir(parents=True, exist_ok=True) | Create directory |
| 🖐️ Touch | path.touch() | Create file (or update timestamp) |
| 📊 Stat | path.stat() | Return file status information |
| 🔐 Chmod | path.chmod(0o755) | Change file permissions |
| 👤 Owner | path.owner() | Return owner login name |
| 👥 Group | path.group() | Return group name of file |
| 🔗 Create symlink | path.symlink_to(target) | Make this path a symlink to target |
| 🔗 Read symlink | path.readlink() | Return target of symlink |
| 🔗 Hard link | path.hardlink_to(target) | Create hard link to target |
| 📝 Change suffix | path.with_suffix('.new') | Return new path with changed suffix |
| 📝 Change name | path.with_name('new') | Return new path with changed filename |
| 📝 Change stem | path.with_stem('new') | Return new path with changed stem |
| 🔗 Match pattern | path.match('*.txt') | Check if path matches glob pattern |
| 📌 Relative to | path.relative_to('/base') | Compute relative path |
| 📌 Is relative to? | path.is_relative_to('/base') | Check if path is relative to another |
| 🌐 URI | path.as_uri() | Return file:// URI |
| 📟 Open | path.open('r') | Open file (same as built-in open) |
https://docs.python.org/3.10/library/pathlib.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
builtins.object
PurePath
Path
PosixPath(Path, PurePosixPath)WindowsPath(Path, PureWindowsPath)PurePosixPathPureWindowsPathPath(*args, **kwargs)
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.
Method resolution order:
Path
PurePath
builtins.object
__enter__(self)__exit__(self, t, v, tb)absolute(self) — 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.chmod(self, mode, *, follow_symlinks=True) — Change the permissions of the path, like os.chmod().exists(self) — Whether this path exists.expanduser(self) — Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser).glob(self, pattern) — Iterate over this subtree and yield all existing files (of any kind, including directories) matching the given relative pattern.group(self) — Return the group name of the file gid.hardlink_to(self, target) — Make this path a hard link pointing to the same file as *target*. Note the order of arguments (self, target) is the reverse of os.link's.is_block_device(self) — Whether this path is a block device.is_char_device(self) — Whether this path is a character device.is_dir(self) — Whether this path is a directory.is_fifo(self) — Whether this path is a FIFO.is_file(self) — Whether this path is a regular file (also True for symlinks pointing to regular files).is_mount(self) — Check if this path is a POSIX mount point.is_socket(self) — Whether this path is a socket.is_symlink(self) — Whether this path is a symbolic link.iterdir(self) — Iterate over the files in this directory. Does not yield any result for the special paths '.' and '..'.lchmod(self, mode) — Like chmod(), except if the path points to a symlink, the symlink's permissions are changed, rather than its target's.link_to(self, target) — Make the target path a hard link pointing to this path. Note this function does not make this path a hard link to *target*, despite the implication of the function and argument names. The order of arguments (target, link) is the reverse of Path.symlink_to, but matches that of os.link. Deprecated since Python 3.10 and scheduled for removal in Python 3.12. Use hardlink_to() instead.lstat(self) — Like stat(), except if the path points to a symlink, the symlink's status information is returned, rather than its target's.mkdir(self, mode=511, parents=False, exist_ok=False) — Create a new directory at this given path.open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None) — Open the file pointed by this path and return a file object, as the built-in open() function does.owner(self) — Return the login name of the file owner.read_bytes(self) — Open the file in bytes mode, read it, and close the file.read_text(self, encoding=None, errors=None) — Open the file in text mode, read it, and close the file.readlink(self) — Return the path to which the symbolic link points.rename(self, 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.replace(self, 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.resolve(self, strict=False) — Make the path absolute, resolving all symlinks on the way and also normalizing it (for example turning slashes into backslashes under Windows).rglob(self, pattern) — Recursively yield all existing files (of any kind, including directories) matching the given relative pattern, anywhere in this subtree.rmdir(self) — Remove this directory. The directory must be empty.samefile(self, other_path) — Return whether other_path is the same or not as this file (as returned by os.path.samefile()).stat(self, *, follow_symlinks=True) — Return the result of the stat() system call on this path, like os.stat() does.symlink_to(self, target, target_is_directory=False) — Make this path a symlink pointing to the target path. Note the order of arguments (link, target) is the reverse of os.symlink.touch(self, mode=438, exist_ok=True) — Create this file with the given access mode, if it doesn't exist.unlink(self, missing_ok=False) — Remove this file or link. If the path is a directory, use rmdir() instead.write_bytes(self, data) — Open the file in bytes mode, write to it, and close the file.write_text(self, data, encoding=None, errors=None, newline=None) — Open the file in text mode, write to it, and close the file.cwd() from builtins.type — Return a new path pointing to the current working directory (as returned by os.getcwd()).home() from builtins.type — Return a new path pointing to the user's home directory (as returned by os.path.expanduser('~')).__new__(cls, *args, **kwargs) — 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.__bytes__(self) — Return the bytes representation of the path. This is only recommended to use under Unix.__eq__(self, other) — Return self==value.__fspath__(self)__ge__(self, other) — Return self>=value.__gt__(self, other) — Return self>value.__hash__(self) — Return hash(self).__le__(self, other) — Return self<=value.__lt__(self, other) — Return self<value.__reduce__(self) — Helper for pickle.__repr__(self) — Return repr(self).__rtruediv__(self, key)__str__(self) — Return the string representation of the path, suitable for passing to system calls.__truediv__(self, key)as_posix(self) — Return the string representation of the path with forward (/) slashes.as_uri(self) — Return the path as a 'file' URI.is_absolute(self) — True if the path is absolute (has both a root and, if applicable, a drive).is_relative_to(self, *other) — Return True if the path is relative to another path or False.is_reserved(self) — Return True if the path contains one of the special names reserved by the system, if any.joinpath(self, *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).match(self, path_pattern) — Return True if this path matches the given pattern.relative_to(self, *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.with_name(self, name) — Return a new path with the file name changed.with_stem(self, stem) — Return a new path with the stem changed.with_suffix(self, 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.__class_getitem__(type) from builtins.typeanchor — 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 the components 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']PosixPath(*args, **kwargs)
Path subclass for non-Windows systems. On a POSIX system, instantiating a Path should return this object.
Method resolution order:
PosixPath
Path
PurePosixPath
PurePath
builtins.object
Inherits all methods from Path and all methods and properties from PurePath (via PurePosixPath). No additional methods are defined.
WindowsPath(*args, **kwargs)
Path subclass for Windows systems. On a Windows system, instantiating a Path should return this object.
Method resolution order:
WindowsPath
Path
PureWindowsPath
PurePath
builtins.object
Inherits all methods from Path and PurePath. Overrides:
is_mount(self) — Check if this path is a POSIX mount point.PurePath(*args)
Base class for manipulating paths without I/O. PurePath represents a filesystem path and offers operations which don't imply any actual filesystem I/O. Depending on your system, instantiating a PurePath will return either a PurePosixPath or a PureWindowsPath object. You can also instantiate either of these classes directly, regardless of your system.
__bytes__(self) — Return the bytes representation of the path. This is only recommended to use under Unix.__eq__(self, other) — Return self==value.__fspath__(self)__ge__(self, other) — Return self>=value.__gt__(self, other) — Return self>value.__hash__(self) — Return hash(self).__le__(self, other) — Return self<=value.__lt__(self, other) — Return self<value.__reduce__(self) — Helper for pickle.__repr__(self) — Return repr(self).__rtruediv__(self, key)__str__(self) — Return the string representation of the path, suitable for passing to system calls.__truediv__(self, key)as_posix(self) — Return the string representation of the path with forward (/) slashes.as_uri(self) — Return the path as a 'file' URI.is_absolute(self) — True if the path is absolute (has both a root and, if applicable, a drive).is_relative_to(self, *other) — Return True if the path is relative to another path or False.is_reserved(self) — Return True if the path contains one of the special names reserved by the system, if any.joinpath(self, *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).match(self, path_pattern) — Return True if this path matches the given pattern.relative_to(self, *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.with_name(self, name) — Return a new path with the file name changed.with_stem(self, stem) — Return a new path with the stem changed.with_suffix(self, 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.__class_getitem__(type) from builtins.type__new__(cls, *args) — 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.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 the components 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']PurePosixPath(*args)
PurePath subclass for non-Windows systems. On a POSIX system, instantiating a PurePath should return this object. However, you can also instantiate it directly on any system.
Method resolution order:
PurePosixPath
PurePath
builtins.object
Inherits all methods, class methods, static methods, and readonly properties from PurePath. No additional members are defined.
PureWindowsPath(*args)
PurePath subclass for Windows systems. On a Windows system, instantiating a PurePath should return this object. However, you can also instantiate it directly on any system.
Method resolution order:
PureWindowsPath
PurePath
builtins.object
Inherits all methods, class methods, static methods, and readonly properties from PurePath. No additional members are defined.
__all__ = ['PurePath', 'PurePosixPath', 'PureWindowsPath', 'Path', 'Po...
/usr/lib/python3.10/pathlib.py
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 12:11 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format