Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

pathlib

pathlib supplies object-oriented filesystem path classes. Use Path for platform-aware filesystem operations and pure path classes for calculations that do not access the filesystem.

Since
3.4

How it works

Use pathlib.Path for object-oriented filesystem paths. Pure path classes calculate path information without filesystem access, while concrete paths also perform filesystem operations; Path selects the concrete path appropriate for the running platform.

Syntax

class pathlib.Path(*pathsegments)

Parameters

NameTypeDescription
*pathsegmentsstr or os.PathLikePath segments or path-like values used to construct the path object.

Returns

A path object representing the supplied filesystem path.

Key points

  • pathlib provides filesystem-path objects whose behavior is designed for different operating systems.
  • Pure path classes handle computational path transformations without performing input/output.
  • Concrete paths add filesystem input/output capabilities to the operations inherited from pure paths.
  • Path chooses a concrete path implementation for the current runtime platform.
  • The primary class is imported from pathlib as Path.

Common mistakes

  • PurePath operations do not access the filesystem; use Path when you need filesystem operations.
  • Concrete paths extend pure paths, so filesystem-aware behavior belongs to concrete path classes.

Example: Find Python files

from pathlib import Path

root = Path(".")
for path in root.glob("*.py"):
    print(path.name)

Creates a platform-aware path and prints the names of Python files in the current directory.

When to use it

Platform-aware path construction

Constructing portable filesystem locations in backend or scripting code.

Pure path manipulation

Combining, inspecting, or transforming path values without filesystem I/O.

Filesystem traversal and I/O

Finding files and performing filesystem operations through concrete paths.

Interview questions

QWhat problem does pathlib solve?

pathlib represents filesystem paths with objects rather than relying only on path strings. Pure paths perform computational path operations without I/O, while concrete paths also provide system calls.

Follow-up: When should you use PurePath instead of Path? Use PurePath when you need path manipulation without accessing the filesystem, such as analyzing a path for another operating system.

QWhat does Path represent?

Path is the main concrete class to use when the appropriate platform-specific path is desired. It instantiates a concrete path for the platform where the program runs.

Follow-up: How can code remain portable across operating systems? Construct paths through Path and its methods rather than embedding platform-specific separators.

QHow do pure and concrete paths differ?

Pure paths are limited to path-handling calculations, whereas concrete paths inherit those operations and add filesystem system calls. This separation lets code manipulate paths without triggering I/O.

Follow-up: Why is this separation useful in testing? Pure path logic can be tested without creating, reading, or inspecting filesystem entries.

  • PurePath
  • PurePosixPath
  • PureWindowsPath
  • Path
  • PosixPath
  • WindowsPath

Source: pathlib — Object-oriented filesystem paths. Checked 2026-10-15.