Skip to main content
The pathlib module provides classes for working with filesystem paths in an object-oriented way. It’s the modern replacement for many os.path operations.

Module Import

Creating Path Objects

Path() - Platform-Specific Path

Path Operators

Use / operator to join paths:

Path Properties

Path Components

Path Types

File Operations

Reading Files

Writing Files

File Information

Directory Operations

Listing Contents

Creating and Removing Directories

Path Manipulation

Joining Paths

Changing Path Components

Resolving Paths

File Operations

Renaming and Moving

Copying Files

Permissions

Special Methods

Path Matching

Practical Examples

Find All Files with Extension

Calculate Directory Size

Backup Files with Timestamp

Clean Up Old Files

Process All Files in Directory

Path Types

Pure Paths (No I/O)

Concrete Paths (With I/O)

Best Practices

Use pathlib instead of os.path for new code:
Use missing_ok=True when deleting:
Always use parents=True when creating nested directories:

Migration from os.path

os

Operating system interfaces

shutil

High-level file operations

glob

Unix-style pathname patterns

tempfile

Temporary files