pathlib
Python's standard library module for working with file paths as objects rather than plain strings.
In more detail
Path("data") / "notes.txt" joins parts with the right separator for the system, and methods such as read_text, write_text, exists, iterdir and glob do common jobs in one call. Gluing path strings with + breaks on another operating system.
Example
from pathlib import Path
for p in Path(".").iterdir():
print(p.name)Related terms
- path — The location of a file or folder written as the folders leading to it, such as /home/ana/notes.txt or C:\Users\ana\notes.txt.
- iterdir() — The Path method that yields the entries, files and folders, inside a directory.
- glob pattern — A pattern that matches names using * for any run of characters, a question mark for one character and [abc] for one of a set.
- directory — A folder: a named container in a filesystem that holds files and other directories.
- tmp_path — A built-in pytest fixture that gives each test its own fresh temporary directory as a pathlib.Path.
- character encoding — The rule for turning text characters into bytes and back, such as UTF-8.
- filesystem — The way an operating system organises files and folders on a disk or in memory.
Taught in
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book
- Shipping: scripts, entry points and git — Hands-on Python: Complete Video Course & Book
- Testing The Filesystem — Automated Testing, TDD & Quality Engineering
- When awk Runs Out: Python Data Types, Modules And Virtual Environments For Admins — CompTIA Linux+ (XK0-006) Exam Prep