Tutorials Logic, IN info@tutorialslogic.com

Python pathlib: Files, Folders, Paths, and Globs

Path Basics

pathlib gives Python an object-oriented way to work with file and folder paths.

A Path object is easier to combine, inspect, and pass around than raw string paths.

Use pathlib when code needs to read files, write reports, scan folders, or build paths that work across operating systems.

Create Paths

Path objects can represent files or folders without immediately opening them.

  • Use / to join path parts.
  • Use exists(), is_file(), and is_dir() to inspect paths.

Join Path Parts

Join Path Parts
from pathlib import Path

base = Path("reports")
file_path = base / "january.txt"

print(file_path)
Output
reports\january.txt

Path joins the folder and file name using the correct path style for the system.

Read and Write

Path has convenient read_text() and write_text() helpers for small text files.

  • Use explicit encoding for text files.
  • Use open() for streaming large files.

Write and Read Text

Write and Read Text
from pathlib import Path

path = Path("note.txt")
path.write_text("Learn pathlib", encoding="utf-8")

print(path.read_text(encoding="utf-8"))
Output
Learn pathlib

write_text() creates or replaces the file, and read_text() reads it back as a string.

Find Files

glob() finds files that match a pattern inside a folder.

  • Use *.py for Python files in one folder.
  • Use rglob() when searching recursively through subfolders.

Path Safety

Path bugs usually come from assuming the current working directory.

  • Use Path(__file__).parent when a script needs files next to itself.
  • Check paths before opening files that may not exist.
Skill check

Can You Build Paths?

5 checks
  • Use Path objects instead of hand-built path strings.
  • Join paths with / between Path objects and names.
  • Use read_text() and write_text() for small text files.
  • Use glob() and rglob() to find matching files.
  • Build reliable paths from script location when needed.

Path Decisions

0 of 2 checked

Q1. What is the pathlib way to combine path parts?

Q2. What should you inspect when relative paths behave differently?

Path Bugs Across Folders

  • Joining paths with string plus

    Use Path objects and the / operator so separators are handled cleanly.
  • Assuming the current folder

    Print Path.cwd() or build paths relative to a known base.
  • Reading before checking existence

    Use exists(), is_file(), or a try/except around file operations.

Try this next

Build a Portable Path

0 of 3 completed

  1. Use Path("reports") / "january.txt" and print it.
  2. Print a message if a path is missing instead of crashing immediately.
  3. Use glob to list all .txt files in a folder.

Questions About pathlib

pathlib is usually clearer for new code because paths become objects with useful methods. os.path is still common in older code.

No. A Path object can point to a location that does not exist yet. Writing or mkdir() creates something.

Avoid it for very large files. Use open() and process the file line by line instead.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.