1. Home
  2. Blog
  3. Software Development

Software Development

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.

9 min read
Python pathlib explained: paths, files and folders the modern way, tested

Key takeaways

  • pathlib.Path represents a path as an object. Join parts with / instead of string concatenation.
  • .name, .stem, .suffix and .parent replace most os.path string tricks.
  • glob searches one folder; rglob searches 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.join in 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" returned False, while Path("a") == Path("a") returned True. 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. Prefer unlink(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

Work with Stack Quora

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.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

Jaccard Similarity: Formula, Python, MinHash

Jaccard similarity measures overlap between two sets as intersection over union. Work the formula, compute it in Python for words…

Read article →
Software Development

Unix Timestamp Explained: Seconds vs ms

A Unix timestamp counts seconds since 1 January 1970 UTC. Learn seconds vs milliseconds, converting in Python, JavaScript and SQL,…

Read article →
DevOps

Git Hooks: Pre-commit and Commit-msg Examples

Git hooks run scripts at key moments such as commit and push. See tested pre-commit, commit-msg and prepare-commit-msg hooks, sharing…

Read article →
Test Automation

API Testing in Python: Pytest + Requests Suite

Build an API testing suite in Python with pytest and requests: fixtures, status codes, headers, JSON schema validation, CRUD flows,…

Read article →
Software Testing

Test Design Techniques: 9 Methods + Examples

Test design techniques help you write fewer tests that catch more bugs. Learn 9 black-box, white-box and experience-based methods with…

Read article →
Databases & SQL

SQL Temporary Table: Create, Use and Drop It

SQL temporary tables store interim results for one session. Learn syntax for SQL Server, PostgreSQL, MySQL, Oracle and SQLite, session…

Read article →
Databases & SQL

SQL CROSS JOIN: Every Combination + Examples

SQL CROSS JOIN returns every combination of rows from two tables. See the row math, product variants, calendar grids that…

Read article →
Software Development

JavaScript Date: Format, Parse, Timezone Bugs

JavaScript Date explained with tested Node.js code: zero-based months, the date-only UTC parsing trap, toISOString, getTimezoneOffset, Intl formatting, date math…

Read article →
Software Development

Python Requests: GET, POST, Timeouts, Retries

Python requests explained with code tested against a local API: GET with params, POST json vs data, headers and auth,…

Read article →
Software Development

Python Deepcopy vs Copy: Fix Nested Lists

Python deepcopy vs shallow copy explained with tested code: aliasing, nested lists, the [[0]*3]*3 trap, memo and cycles, custom __deepcopy__,…

Read article →
Artificial Intelligence

TF-IDF Explained: Formula and Worked Example

TF-IDF scores a word by how often it appears in a document and how rare it is across all documents….

Read article →
Software Development

Base64 Encoding Explained, With Python Code

Base64 encoding turns bytes into 64 safe text characters. See the bit-level steps, padding, URL-safe base64, the 33% size cost,…

Read article →
DevOps

Git Tag: Create, Push and Delete Release Tags

Git tag explained with tested commands: annotated vs lightweight tags, listing and sorting, pushing with follow-tags, deleting local and remote…

Read article →
Test Automation

Pytest Coverage: pytest-cov and Branch Gaps

Measure pytest coverage with pytest-cov: term-missing reports, branch coverage, HTML reports, pyproject settings and a fail-under gate for CI, with…

Read article →
Software Testing

Error Guessing in Testing: Checklist + Demo

Error guessing uses tester experience to predict where code breaks. Get a ready checklist of risky inputs, a step by…

Read article →
Databases & SQL

SQL LAG and LEAD: Compare Rows, 5 Examples

SQL LAG and LEAD read values from the previous or next row without a self join. Five tested examples: change,…

Read article →
Databases & SQL

SQL STRING_AGG: Combine Rows Into One String

SQL STRING_AGG joins values from many rows into one string per group. See syntax for PostgreSQL, SQL Server, MySQL GROUP_CONCAT,…

Read article →
Software Development

JavaScript Classes: Private Fields and Extends

JavaScript classes explained with tested Node.js code: constructors, methods, #private fields, getters, static members, extends and super, plus the errors…

Read article →
Artificial Intelligence

BM25 Explained: How Keyword Search Ranks Results, With Python Code and Hybrid Search

BM25 explained with a pure Python implementation we ran: the formula, IDF, term saturation, the k1 and b parameters, why…

Read article →
DevOps

Git Bisect Explained: Find the Commit That Broke Your Code, Manually or Automatically

Git bisect explained with a tested repository: find the one bad commit among 41 in 5 test runs, automate it…

Read article →
Test Automation

pytest Markers Explained: Custom Marks, skip, skipif, xfail and -m Filters

pytest markers explained with a tested suite: register custom marks, run only smoke tests with -m, combine filters with and,…

Read article →
Software Testing

Positive vs Negative Testing: Differences, 24 Real Test Cases and a Checklist

Positive vs negative testing explained with a real signup form: 6 positive and 18 negative test cases run in pytest,…

Read article →
Databases & SQL

SQL ORDER BY Multiple Columns: ASC, DESC, NULLS LAST, Case and Custom Order

SQL ORDER BY with multiple columns explained with tested queries: mixed ASC and DESC, tie-breakers, NULLS FIRST and LAST, case-insensitive…

Read article →
Databases & SQL

SQL Running Total: SUM OVER, Per-Group Totals, Moving Sums and the RANGE Trap

How to calculate a SQL running total with SUM() OVER: per-group totals, daily cumulative sums, moving sums, percent of total,…

Read article →
Artificial Intelligence

Cosine Similarity Explained: Formula, Worked Example and Python Code

Cosine similarity explained simply: the formula, a worked example by hand, pure Python and NumPy code, cosine vs Euclidean distance…

Read article →
DevOps

Git Squash Commits: 4 Easy Ways, Tested Step by Step (rebase -i, reset, merge)

Git squash commits explained with 4 tested methods: interactive rebase, reset –soft, merge –squash and fixup with autosquash. Before-and-after logs,…

Read article →
Test Automation

pytest parametrize Explained: 7 Patterns with Tested Examples

pytest parametrize explained with 7 tested patterns: basic cases, readable ids, stacked decorators, pytest.param with xfail and skip, indirect fixtures…

Read article →
Software Testing

Retesting vs Regression Testing: 8 Key Differences with a Real Bug Fix Example

Retesting vs regression testing explained with a real bug fix run in pytest: retesting confirmed the fix, the regression run…

Read article →
Artificial Intelligence

RAG Chunking Strategies Explained: Fixed, Overlap, Sentence, Recursive and Headings, Measured

RAG chunking strategies compared on one document with tiktoken 0.14: fixed-size, overlap, sentence packing, recursive splitting and heading-based chunks, measuring…

Read article →
DevOps

Git Stash Explained: Save, Apply, Pop and Untracked Files, Tested

Git stash explained with commands we ran on Git 2.56: push with a message, untracked files, list and show, apply…

Read article →
Software Testing

Pairwise Testing Explained: How All-Pairs Cuts 216 Tests to 13, Tested

Pairwise testing explained with a generator we ran in Python 3.13: a checkout with 216 combinations covered by 13 tests,…

Read article →
Test Automation

Test Doubles Explained: Dummy, Stub, Spy, Mock and Fake, with Tested Python Code

Test doubles explained with one Python example run in pytest 9.2: dummy, stub, spy, mock and fake, when to use…

Read article →
Artificial Intelligence

LLM Context Window Explained: Tokens, Limits and How to Fit More, Measured

The LLM context window explained with token counts we measured using tiktoken: words vs tokens for prose, code and Hindi,…

Read article →
DevOps

Git Cherry-Pick Explained: Copy the Commit You Need, Safely, Tested

Git cherry-pick explained with commands we ran in Git 2.56: copying a hotfix to a release branch, new hashes and…

Read article →
Test Automation

Snapshot Testing Explained: How It Works, When It Helps and When It Hurts

Snapshot testing explained with a pytest and syrupy demo we ran: creating snapshots, reading the diff after a code change,…

Read article →
Software Testing

Mutation Testing Explained: Why 100% Coverage Still Missed Our Bug

Mutation testing explained with a small mutation tool we built and ran: 14 mutants, a test suite with 100% line…

Read article →