0Pricing
Python Academy · Lesson

Working with File Paths using pathlib

Navigate and manipulate file paths with the modern pathlib module.

Working with File Paths using pathlib is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Path Joining with /

p = Path('data') / 'subdir' / 'file.txt' uses the / operator to join path components — clean and cross-platform.
from pathlib import Path
p = Path('data') / 'sub' / 'file.txt'
print(p)

Path Properties

p.name gives the filename. p.stem gives name without extension. p.suffix gives the extension. p.parent gives the directory.
from pathlib import Path
p = Path('data/report.csv')
print(p.name, p.stem, p.suffix, p.parent)

Checking Existence

p.exists() True if path exists. p.is_file() for files. p.is_dir() for directories.
from pathlib import Path
p = Path('/tmp')
print(p.exists(), p.is_dir(), p.is_file())

Reading and Writing

p.read_text(encoding='utf-8') reads the file as a string. p.write_text('content') writes a string.
from pathlib import Path
p = Path('/tmp/test_pathlib.txt')
p.write_text('hello pathlib')
print(p.read_text())

Iterating Directory Contents

for child in p.iterdir(): yields Path objects for all items. p.glob('*.py') matches patterns.
from pathlib import Path
for child in Path('/tmp').iterdir():
    if child.suffix == '.txt':
        print(child.name)
        break

Recursive Glob

p.rglob('*.json') recursively finds all .json files under p. Much cleaner than os.walk().
from pathlib import Path
for f in Path('/tmp').rglob('*.txt'):
    print(f.name)
    break

Creating Directories

p.mkdir(parents=True, exist_ok=True) creates the directory and all parents. exist_ok=True prevents FileExistsError.
from pathlib import Path
Path('/tmp/test_dir/sub').mkdir(parents=True, exist_ok=True)
print('created')

Renaming and Deleting

p.rename(new) renames the file. p.unlink() deletes a file. p.rmdir() removes an empty directory.
from pathlib import Path
p = Path('/tmp/test_pathlib.txt')
if p.exists():
    p.unlink()
    print('deleted')

Absolute and Relative Paths

p.resolve() returns the absolute path with symlinks resolved. p.relative_to(base) computes a relative path.
from pathlib import Path
print(Path('.').resolve())

Quick Check

Which operator joins Path components in pathlib?

Recap

pathlib Path: / joins parts. .name/.stem/.suffix/.parent for components. read_text/write_text for I/O. mkdir, unlink, glob for filesystem operations.

Keep Going

Great work! Move on to the next lesson to keep progressing.

Frequently asked questions

Is the “Working with File Paths using pathlib” lesson free?

Yes — the full text of “Working with File Paths using pathlib” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Working with File Paths using pathlib”?

Navigate and manipulate file paths with the modern pathlib module. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Working with File Paths using pathlib” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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