On this page
  1. pathlib.Path
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

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

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

REPL Inspect

from pathlib import Path
help(Path)

Linked from

Related notes