Python pathlib Explained: Paths, Files and Folders the Modern Way, Tested
Python pathlib turns file paths into objects you can join with a slash, inspect, glob and read in one line. Learn the everyday Path methods, the Windows and Linux differences, and the traps we hit while running every example.
pathlib.Pathrepresents a path as an object. Join parts with/instead of string concatenation..name,.stem,.suffixand.parentreplace mostos.pathstring tricks.globsearches one folder;rglobsearches all subfolders.- pathlib is easier to read but slower to build: 100,000 joins took 262 ms with pathlib against 77 ms with
os.path.joinin our run.
File paths look simple until your script moves from a Windows laptop to a Linux CI server and every hard-coded backslash breaks. Python pathlib solves this by treating a path as an object rather than a string. You build it with the / operator, ask it questions such as “what is the extension?”, and read or write the file through methods on the same object.
We ran every example below with Python 3.13.6 on Windows, inside a temporary folder. Outputs are copied from that run, with the temporary folder shown as relative paths. If you are new to files in general, start with our Python file handling guide.
What is pathlib?
pathlib is a standard library module, available since Python 3.4, so there is nothing to install. Its main class is Path. When you create a Path, Python picks the right flavour for your operating system: WindowsPath on Windows and PosixPath on Linux and macOS.
from pathlib import Path
p = Path("reports") / "2026" / "nov" / "sales.final.csv"
print(p.as_posix())
reports/2026/nov/sales.final.csv
Joining paths with the / operator
The / operator is the headline feature. It joins parts with the correct separator for the system. as_posix() prints forward slashes everywhere, which is handy for logs and URLs.
One trap: you cannot add a string and a path with +. We tried "data/" + Path("x.csv") and got:
TypeError: can only concatenate str (not "WindowsPath") to str
Use Path("data") / "x.csv" instead, or convert explicitly with str(path) when an older library insists on a string. Most modern libraries, including open(), accept Path objects directly.
Name, stem, suffix and parent
These properties answer the questions you used to solve with split and slicing:
print(p.name, p.stem, p.suffix, p.suffixes)
print(p.parent.as_posix(), p.parts)
print(p.with_suffix(".json").name, p.with_stem("sales").name, p.with_name("x.txt").name)
sales.final.csv sales.final .csv ['.final', '.csv']
reports/2026/nov ('reports', '2026', 'nov', 'sales.final.csv')
sales.final.json sales.csv x.txt
Notice the details. .stem removes only the last suffix, so a file called sales.final.csv keeps .final in its stem. .suffixes lists all of them, which helps with names such as backup.tar.gz. The with_ methods return a new path and never rename anything on disk.
| Need | os.path way | pathlib way |
|---|---|---|
| File name | os.path.basename(s) |
p.name |
| Folder | os.path.dirname(s) |
p.parent |
| Extension | os.path.splitext(s)[1] |
p.suffix |
| Join | os.path.join(a, b) |
Path(a) / b |
| Exists | os.path.exists(s) |
p.exists() |
Reading and writing files
For small files, read_text and write_text open, read or write, and close the file in one call:
f = root / "notes.txt"
print(f.write_text("hello\nworld\n", encoding="utf-8"))
print(f.read_text(encoding="utf-8").splitlines())
12
['hello', 'world']
write_text returned 12, the number of characters written. Always pass encoding="utf-8"; without it, Python uses the system default, which differs between machines. For large files or line-by-line work, use f.open() inside a with block, as explained in our guide to Python context managers. There are also read_bytes and write_bytes for binary data such as images.
The newline surprise on Windows
After writing those 12 characters, we appended one more line, "again\n", and checked the size:
with f.open("a", encoding="utf-8") as fh:
fh.write("again\n")
print(f.stat().st_size) # 21
We wrote 18 characters in total but the file was 21 bytes. On Windows, text mode turns each \n into \r\n, and we wrote three line breaks. On Linux and macOS no translation happens, so the file would be 18 bytes (we ran this part on Windows only). If a test compares file sizes or checksums across systems, pass newline="\n" when writing, or compare text instead of bytes.
Tip: stat().st_size counts bytes, not characters. Non-English text in UTF-8 takes more than one byte per character, so the two numbers differ there too.
Creating folders safely
(root / "logs" / "old").mkdir(parents=True, exist_ok=True)
(root / "logs" / "old").mkdir(parents=True, exist_ok=True) # no error
(root / "x" / "y").mkdir() # FileNotFoundError
parents=True creates any missing folders in between, and exist_ok=True makes the call safe to repeat. Without parents, creating x/y failed because x did not exist. Use both flags in setup code that may run more than once.
Finding files with glob and rglob
We created a.log, b.log and c.txt in logs, and d.log in logs/old:
print(sorted(x.name for x in (root / "logs").glob("*.log")))
print(sorted(x.relative_to(root).as_posix() for x in root.rglob("*.log")))
print(sorted(x.name for x in (root / "logs").iterdir()))
['a.log', 'b.log']
['logs/a.log', 'logs/b.log', 'logs/old/d.log']
['a.log', 'b.log', 'c.txt', 'old']
glob("*.log") stayed in one folder, rglob went into old as well, and iterdir listed everything, folders included. The results come back in no guaranteed order, so we wrapped them in sorted(); do the same in tests, or they may pass on one machine and fail on another. To find the largest file, combine rglob with stat: max(root.rglob("*.*"), key=lambda x: x.stat().st_size).
Renaming and deleting
new = f.rename(root / "notes.md")
print(new.name, f.exists()) # notes.md False
(root / "gone.txt").unlink(missing_ok=True) # no error
rename returns the new path, and the old Path object now points at nothing. unlink(missing_ok=True) deletes a file without failing if it is already gone, which is ideal for clean-up code. Deleting a folder needs rmdir(), which only works on empty folders; for a full tree, use shutil.rmtree, and handle errors as shown in our Python exception handling guide.
Relative and absolute paths
print((root / "logs" / "a.log").relative_to(root).as_posix()) # logs/a.log
print((root / "logs" / "..").resolve() == root.resolve()) # True
print((root / "logs").is_relative_to(root)) # True
relative_to strips a base folder, resolve makes a path absolute and removes .. segments, and is_relative_to checks containment. That last check matters for security: if users supply file names, resolve the final path and confirm it is still inside your upload folder before reading it.
Path.home() and Path.cwd() give the home and current folders. Avoid relying on the current folder in scripts; build paths from the script’s own location with Path(__file__).parent, or read locations from environment variables.
Windows vs POSIX paths
from pathlib import PurePosixPath, PureWindowsPath
print(PurePosixPath("a", "b", "c.txt"), PureWindowsPath("a", "b", "c.txt"))
a/b/c.txt a\b\c.txt
The “pure” classes never touch the disk, so you can work with Linux paths on Windows and the reverse. That is useful when a Windows tool builds paths for a Linux server. Python 3.13 also added full_match, which matches a whole path against a pattern; PurePosixPath("src/app/main.py").full_match("src/**/*.py") returned True.
pathlib vs os.path: is it slower?
Yes, for building paths. We joined three parts 100,000 times: os.path.join took 77 ms and Path("a") / "b" / "c.txt" took 262 ms. Each / creates a new object. In real programs this rarely matters, because reading the disk costs far more than building the path. Choose pathlib for readability, and only fall back to os.path in hot loops that build millions of paths.
pathlib in tests and automation
pytest’s built-in tmp_path fixture hands each test a fresh Path to a temporary folder, so pathlib is the natural way to create input files and check outputs. Combine it with write_text to build fixtures in one line, then assert with read_text or exists. See our pytest fixtures guide for how fixtures work, and our Python JSON guide for reading config files you locate with pathlib.
Common pathlib mistakes
A few small traps account for most pathlib bugs we see in code reviews. We checked each behaviour below on Python 3.13:
- Comparing a path with a string.
Path("a") == "a"returnedFalse, whilePath("a") == Path("a")returnedTrue. Convert one side before comparing. - Expecting
~to work.Path("~/x")keeps the tilde as a literal folder name. Call.expanduser()to turn it into your real home folder. - Checking then acting.
if p.exists(): p.unlink()can still fail if another process deletes the file in between. Preferunlink(missing_ok=True)or catch the error. - Forgetting the encoding. Text read without
encoding="utf-8"can break on another machine.
Final thoughts
Python pathlib makes file code shorter and more portable. Use / to join, properties instead of string slicing, mkdir(parents=True, exist_ok=True) for setup, and rglob for searching. Watch for the traps we hit: string concatenation errors, Windows newline translation, unordered glob results and the small speed cost of creating many objects.
Frequently asked questions
What is pathlib in Python?
pathlib is a built-in module that represents file system paths as objects. It lets you join, inspect, search, read and write paths with methods instead of string functions.
Should I use pathlib or os.path?
Use pathlib for most new code because it is clearer and works across systems. os.path is faster for building huge numbers of paths: 77 ms against 262 ms for 100,000 joins in our test.
What is the difference between glob and rglob?
glob matches files in one folder, while rglob searches that folder and all subfolders. In our test, rglob found a log file inside a nested folder that glob missed.
How do I get a file extension with pathlib?
Use path.suffix for the last extension, such as .csv, and path.suffixes for all of them. path.stem gives the name without the last extension.
Why is my file bigger than the text I wrote on Windows?
Text mode on Windows writes each newline as two bytes. Our 18 characters with three newlines became 21 bytes. Pass newline set to a single newline character to keep sizes equal.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.