pathlib — filesystem paths

Python pathlib.Path for joining, inspecting, and reading paths, with reproducible examples that do not depend on a local working directory.

モジュールページの本文は現在英語です。

pathlib is a standard-library module. Path objects represent filesystem paths and also provide I/O helpers such as read_text() and write_text(). Pure path operations, such as joining parts or reading a suffix, do not touch the filesystem.

Examples on this page use as_posix() so the printed separators stay stable on Windows and Unix. resolve(), cwd(), and exists() depend on the machine and are described, not used for expected output.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

Constructing and inspecting paths

The / operator joins path segments. Path.parts, name, stem, and suffix are computed from the path string and do not require the file to exist.

Path joining and parts

Path(*pathsegments)
PurePath.joinpath(*pathsegments)
PurePath.with_name(name)
PurePath.with_suffix(suffix)

Build and inspect a path without reading the filesystem.

  • Path is a concrete path and can perform I/O. PurePosixPath and PureWindowsPath are the corresponding pure classes.
  • Use as_posix() when you need a stable string in examples or tests. str(path) uses the current operating system separator.
python
from pathlib import Path

path = Path("src") / "app" / "main.py"
print(path.as_posix())
print(path.name, path.stem, path.suffix)
print(path.parts)
print(path.with_name("test.py").as_posix())
print(path.with_suffix(".md").as_posix())

Expected output

src/app/main.py
main.py main .py
('src', 'app', 'main.py')
src/app/test.py
src/app/main.md

Official documentation →

Reading and writing text

read_text() and write_text() open the file, transfer the data, and close it. In Python 3.13, both methods accept encoding, errors, and newline. If encoding is omitted, the default encoding of open() is used, which is locale-dependent. Prefer an explicit encoding, usually "utf-8", when the file format is known.

Path.read_text and Path.write_text

Path.read_text(encoding=None, errors=None, newline=None)
Path.write_text(data, encoding=None, errors=None, newline=None)

Read or write the entire file as text. The temporary directory in this example is discarded and is not printed, so the output stays reproducible.

ParameterDescription
dataThe string passed to write_text().
encodingText encoding. None selects the default used by open().
errorsHow to handle encoding and decoding errors, as in open().
newlineControls newline translation, as in open(). This argument is present on these methods in Python 3.13.

Returns: read_text() returns a str. write_text() returns the number of characters written.

Raises

  • FileNotFoundError — Raised by read_text() when the path does not exist.
  • OSError — Raised for other filesystem errors, including a missing parent directory on write.
  • iterdir() yields direct children. glob("**/*.py") walks recursively. Both require a real directory and depend on local files.
  • For new code, prefer pathlib over os.path when you want object-oriented paths. os.path remains appropriate when you already have path strings and only need a single function.
python
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as folder:
    path = Path(folder) / "notes.txt"
    written = path.write_text("hello\n", encoding="utf-8")
    print(written)
    print(path.read_text(encoding="utf-8"), end="")
    print(path.name)

Expected output

6
hello
notes.txt

Official documentation →