pathlib vs os.path: Which Should You Use in Python?
pathlib vs os.path compared with side-by-side examples: joining paths, reading files, globbing and walking directories. Use pathlib unless you have a reason.
Use pathlib for new code. It is the modern, object-oriented way to work with file paths, and it replaces most of what os.path, parts of os, glob and file-reading boilerplate do with one readable API.
Side by side
| Task | os.path |
pathlib |
|---|---|---|
| Join paths | os.path.join("a", "b") |
Path("a") / "b" |
| Check exists | os.path.exists(p) |
p.exists() |
| File name | os.path.basename(p) |
p.name |
| Extension | os.path.splitext(p)[1] |
p.suffix |
| Parent folder | os.path.dirname(p) |
p.parent |
| Absolute path | os.path.abspath(p) |
p.absolute() (see note) |
| Read a file | open(p).read() |
p.read_text() |
| List matches | glob.glob("*.py") |
Path(".").glob("*.py") |
Two differences to know before you translate code line by line. Path.resolve() follows symbolic links and returns the real path, whereas os.path.abspath() only makes the path absolute and tidies .. segments as text without touching the file system; Path.absolute() makes a path absolute but does not collapse .., so use resolve() when you want the real location and os.path.abspath() when you want the text-only behaviour. And glob.glob() returns a list, while Path.glob() returns an iterator, so wrap it in list() if you need to count or reuse the results. Also, os.path.basename("a/b/") returns an empty string because of the trailing slash, while Path("a/b/").name returns "b".
The resolve() difference is easy to check. With a symlink link pointing at a folder elsewhere, os.path.abspath("link/../f.txt") returned a path that did not exist, because it tidied link/.. as text, while Path("link/../f.txt").resolve() followed the link and returned the real file. These examples were run on Python 3.13.14 and 3.15.2.
Why pathlib reads better
import os
config = os.path.join(os.path.expanduser("~"), ".config", "app", "settings.toml")
if os.path.exists(config):
with open(config, encoding="utf-8") as f:
text = f.read()from pathlib import Path
config = Path.home() / ".config" / "app" / "settings.toml"
if config.exists():
text = config.read_text(encoding="utf-8")The second version is shorter, reads left to right, and the / operator uses the correct separator on every operating system, which matters on Windows.
Common pathlib recipes
from pathlib import Path
root = Path("project")
# create a folder tree
(root / "data" / "raw").mkdir(parents=True, exist_ok=True)
# write and read text
(root / "notes.txt").write_text("hello", encoding="utf-8")
# find files recursively
for py_file in root.rglob("*.py"):
print(py_file.relative_to(root))
# change an extension
report = Path("report.csv").with_suffix(".json")On Python 3.12 and later, Path.walk() provides the equivalent of os.walk; it yields (directory, subdirectory names, file names). A file such as archive.tar.gz has suffix .gz and suffixes ['.tar', '.gz'], the same last extension that os.path.splitext gives.
Working with code that wants strings
Path objects implement the os.PathLike protocol, so open(), shutil and most of the standard library accept them directly. When a library insists on a string, use str(path) or os.fspath(path).
When os.path is still fine
- Older code you are not rewriting. Mixing both is harmless.
- Pure string manipulation where you never touch the file system.
- Hot loops where creating a
Pathobject per item adds up, which is rare. Measure before you rewrite.
os still has its place for things pathlib doesn’t cover, such as environment variables and process details.
Related reading
Both live in the Python standard library, which is worth skimming for other modules that replace third-party packages. For clean, readable code in general, see how to write clean Python code.
Frequently Asked Questions
Should I use pathlib or os.path?
Use pathlib for new code. Its Path objects combine joining, inspecting, reading and globbing in one readable API. os.path still works and is fine in older code, but it treats paths as plain strings.
Is pathlib faster than os.path?
Not in micro-benchmarks. Plain os.path string functions avoid creating objects, and in a quick test on Python 3.15 joining two segments with os.path.join was about four times faster than building a Path. The absolute difference is far too small to matter next to the file system itself, so choose on readability.
How do I join paths with pathlib?
Use the / operator: Path(‘data’) / ‘raw’ / ‘file.csv’. It uses the right separator for your operating system, so the code works on Windows, macOS and Linux.
Can I use pathlib with open()?
Yes. Path objects implement the os.PathLike protocol, so open(path), shutil functions and most of the standard library accept them. Path also has its own read_text and write_text methods.
How do I walk a directory tree with pathlib?
Use path.rglob(’*’) to match files recursively, or path.walk() on Python 3.12 and later for a directory-by-directory walk like os.walk.