# pydoc > pathlib

---
type: CommandReference
command: pathlib
mode: pydoc
section: ""
source: pydoc3
---

## Quick Reference
- `Path('file.txt').read_text()` — read entire file as string
- `Path('file.txt').write_text('hello')` — write string to file
- `Path('dir').mkdir(parents=True, exist_ok=True)` — create directory tree
- `Path('.').glob('*.py')` — iterate over matching files in directory
- `Path('.').rglob('*.py')` — recursive glob
- `Path.home() / 'config'` — build path to home config
- `Path.cwd()` — current working directory
- `Path('src').rename('dest')` — rename or move a file/directory

## Name
pathlib — Object-oriented filesystem paths

## Synopsis
python
from pathlib import Path
## Classes

### `Path(*args, **kwargs)`
Concrete path class that can make system calls. On instantiation returns a `PosixPath` or `WindowsPath` depending on the platform. Inherits all pure path operations from `PurePath`.

**Class methods:**
- `cwd()` — return a new path pointing to the current working directory
- `home()` — return a new path pointing to the user’s home directory

**File I/O:**
- `read_text(encoding=None, errors=None)` — open in text mode, read, close
- `read_bytes()` — open in bytes mode, read, close
- `write_text(data, encoding=None, errors=None, newline=None)` — open in text mode, write, close
- `write_bytes(data)` — open in bytes mode, write, close
- `open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)` — return a file object like built-in `open()`

**Directory operations:**
- `mkdir(mode=0o777, parents=False, exist_ok=False)` — create a directory
- `rmdir()` — remove empty directory
- `iterdir()` — iterate over directory contents (excluding `.` and `..`)
- `glob(pattern)` — iterate over existing files matching a relative pattern
- `rglob(pattern)` — recursive `glob`

**File operations:**
- `rename(target)` — rename to `target` (relative to cwd)
- `replace(target)` — rename, overwriting if target exists
- `unlink(missing_ok=False)` — remove file or symbolic link
- `touch(mode=0o666, exist_ok=True)` — create file with given mode
- `symlink_to(target, target_is_directory=False)` — make this path a symlink to `target`
- `hardlink_to(target)` — make this path a hard link to `target`
- `link_to(target)` — **deprecated** (use `hardlink_to()`)

**Status & type checks:**
- `exists()` — whether the path exists
- `is_file()` — regular file (or symlink to one)
- `is_dir()` — directory
- `is_symlink()` — symbolic link
- `is_block_device()`, `is_char_device()`, `is_fifo()`, `is_socket()`, `is_mount()`
- `stat(*, follow_symlinks=True)` — return `os.stat_result`
- `lstat()` — like `stat()` but without following symlinks
- `owner()` — login name of file owner
- `group()` — group name of file gid
- `samefile(other_path)` — whether `other_path` points to the same file

**Permissions:**
- `chmod(mode, *, follow_symlinks=True)` — change permissions
- `lchmod(mode)` — change permissions of symlink itself

**Other:**
- `absolute()` — absolute version without normalization (keep `.` and `..`)
- `resolve(strict=False)` — absolute path with all symlinks resolved and normalized
- `expanduser()` — expand `~` and `~user` constructs
- `readlink()` — return the path a symlink points to
- `__enter__()` / `__exit__()` — context manager support (e.g., for `with`)

**Inherited from `PurePath` (path manipulation without I/O):**
- `with_name(name)` — replace final component name
- `with_suffix(suffix)` — replace or add/remove suffix
- `with_stem(stem)` — replace final component stem
- `joinpath(*args)` — combine paths
- `relative_to(*other)` — return relative path; raises `ValueError` if not possible
- `is_relative_to(*other)` — return `True` if path is relative to `other`
- `match(path_pattern)` — `True` if path matches the glob-style pattern
- `as_uri()` — return path as `file://` URI
- `as_posix()` — string with forward slashes
- `is_absolute()` — `True` if path is absolute
- `is_reserved()` — `True` if path contains a reserved name (Windows)
- `__truediv__` — join paths using `/` operator
- `__str__`, `__bytes__`, `__fspath__`, `__eq__`, `__hash__`, etc.

**Readonly properties (inherited from `PurePath`):**
`name`, `stem`, `suffix`, `suffixes`, `parts`, `parent`, `parents`, `anchor`, `drive`, `root`

### `PurePath(*args)`
Base class for pure path manipulation without I/O. On instantiation returns a `PurePosixPath` or `PureWindowsPath`. Provides all the path manipulation methods listed above.

### `PosixPath(*args, **kwargs)`
`Path` subclass for POSIX systems. Inherits all methods from `Path` and `PurePosixPath`.

### `WindowsPath(*args, **kwargs)`
`Path` subclass for Windows systems. Inherits all methods from `Path` and `PureWindowsPath`; overrides `is_mount()`.

### `PurePosixPath(*args)`
`PurePath` subclass for POSIX systems. Inherits all pure methods.

### `PureWindowsPath(*args)`
`PurePath` subclass for Windows systems. Inherits all pure methods.

## See Also
- [pathlib — Object-oriented filesystem paths](https://docs.python.org/3/library/pathlib.html)