Skip to main content

Python pathlib: Modern File Path Handling

Intermediate20 min5 exercises70 XP
0/5 exercises

Imagine navigating to a friend's house. You could follow vague paper directions like "turn left at the big tree, then right at the red mailbox." Or you could use GPS, which understands addresses, calculates routes, and handles all the tricky details for you.

Python's pathlib module is like GPS for file paths. The old way (os.path) treats paths as plain strings and requires you to manually juggle slashes, extensions, and platform differences. pathlib gives you smart Path objects that understand what paths are and how they work.

In this tutorial, you'll learn how to create, inspect, join, and search paths using pathlib. Since we're running in the browser, we'll focus on the path manipulation features that work without a real filesystem.

How Do You Create a Path Object?

Import PurePosixPath from pathlib and pass it a string. A PurePosixPath is a path object that works with Unix-style forward slashes and doesn't need a real filesystem.

Creating PurePosixPath objects
Loading editor...

On your own computer, you'd normally use Path (which detects your operating system). Here we use PurePosixPath because it works everywhere, including in the browser. The methods and properties are the same.

What Can You Learn from a Path Object?

Path objects have useful properties that let you extract parts of a file path without any string splitting or slicing.

Extracting parts of a path
Loading editor...

Let's break down what each property gives you:

  • `name` — The final file or folder name: main.py
  • `stem` — The name without the extension: main
  • `suffix` — The file extension including the dot: .py
  • `parent` — The directory containing this file: /home/alice/projects/app
  • `parts` — A tuple of every piece of the path
  • Handling files with multiple extensions
    Loading editor...

    Walking up the directory tree

    You can chain .parent to go up multiple levels, or use the .parents property to get all ancestor directories.

    Navigating up with .parent and .parents
    Loading editor...

    How Do You Join Paths Together?

    One of the best features of pathlib is the / operator for joining paths. Instead of worrying about slashes, you just use / between Path objects or strings.

    Joining paths with the / operator
    Loading editor...

    The / operator automatically handles the separator for you. No more worrying about whether to add a slash or not.

    Old way with os.path.join()
    import os
    path = os.path.join('/home', 'alice', 'docs')
    # String manipulation, easy to mess up
    Modern way with pathlib
    from pathlib import PurePosixPath
    path = PurePosixPath('/home') / 'alice' / 'docs'
    # Clean, readable, type-safe

    Changing file names and extensions

    Path objects have methods to swap out the file name or extension, which is handy for creating related files.

    Changing names and extensions
    Loading editor...

    How Do You Search for Files with Glob Patterns?

    On a real filesystem, Path objects can search for files using glob patterns — simple wildcard expressions. The * matches any sequence of characters, and ** matches any number of directories.

    Since we can't access a real filesystem here, let's learn the patterns and practice with PurePosixPath.match(), which checks if a path matches a glob pattern.

    Matching paths with glob patterns
    Loading editor...

    Common glob patterns you'll use:

  • *.py — All Python files
  • *.txt — All text files
  • test_* — Files starting with test_
  • **/*.py — All Python files in any subdirectory
  • data/* — Everything directly inside the data folder
  • Why Choose pathlib Over os.path?

    Before pathlib (added in Python 3.4), everyone used os.path for file path manipulation. Both work, but pathlib is generally better for new code.

    os.path (old way)
    import os
    
    path = '/home/alice/report.txt'
    print(os.path.basename(path))    # report.txt
    print(os.path.dirname(path))     # /home/alice
    print(os.path.splitext(path)[1]) # .txt
    new = os.path.join('/home', 'bob', 'file.txt')
    pathlib (modern way)
    from pathlib import PurePosixPath
    
    path = PurePosixPath('/home/alice/report.txt')
    print(path.name)    # report.txt
    print(path.parent)  # /home/alice
    print(path.suffix)  # .txt
    new = PurePosixPath('/home') / 'bob' / 'file.txt'

    Key advantages of pathlib:

  • Readable — Properties like .name and .parent are clearer than os.path.basename() and os.path.dirname().
  • Chainable — You can chain operations: path.parent / 'sibling.txt'.
  • Object-oriented — Path objects carry their data with them instead of passing strings to functions.
  • Cross-platform — Automatically handles / vs \ on different operating systems.
  • Chaining pathlib operations
    Loading editor...

    What Is a Common pathlib Pattern?

    Here's a practical pattern: organizing files by their extension. This shows how pathlib makes file organization logic clean and readable.

    Organizing files by extension
    Loading editor...

    Practice Exercises

    Extract Path Components
    Write Code

    Given the path /home/user/documents/essay.txt, create a PurePosixPath and print the file name, the suffix (extension), and the parent directory, each on its own line.

    Expected output:

    essay.txt
    .txt
    /home/user/documents
    Loading editor...
    Predict the Joined Path
    Predict Output

    What does this code print? Think about how the / operator joins paths.

    from pathlib import PurePosixPath
    
    base = PurePosixPath('/home/alice')
    result = base / 'projects' / 'app' / 'main.py'
    print(result)
    Loading editor...
    Change File Extension
    Write Code

    Given the path /data/report.csv, use pathlib to create a new path with the extension changed to .json. Print both the original and new path.

    Expected output:

    Original: /data/report.csv
    Converted: /data/report.json
    Loading editor...
    Fix the Path Joining Bug
    Fix the Bug

    This code tries to build a path by concatenating strings, but it produces an incorrect result. Fix it to use pathlib's / operator.

    Expected output:

    /home/user/docs/notes.txt
    Loading editor...
    Filter Files by Extension
    Write Code

    Given a list of file paths, use pathlib to filter only the .py files and print their stem (filename without extension), one per line, in alphabetical order.

    File list: ['app/main.py', 'app/utils.py', 'app/data.csv', 'app/config.json', 'app/test_app.py']

    Expected output:

    main
    test_app
    utils
    Loading editor...