0Pricing
Python Academy · Lesson

Working with File Paths using pathlib

Navigate and manipulate file paths with the modern pathlib module.

Introduction

pathlib provides an object-oriented interface to filesystem paths, replacing brittle os.path string manipulation.

Creating a Path

from pathlib import Path; p = Path('data/file.txt') creates a path object. Works on all platforms.
from pathlib import Path
p = Path('data/file.txt')
print(p)

All lessons in this course

  1. Opening and Reading Files
  2. Writing and Appending to Files
  3. Using the with Statement for Files
  4. Working with File Paths using pathlib
← Back to Python Academy