0Pricing
Python Academy · Lesson

The Path Object

Build paths the modern way.

The Path Object is a free Python Academy lesson on CoddyKit — lesson 1 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.

Modern Paths with pathlib

The pathlib module represents filesystem paths as objects instead of plain strings. A Path object knows how to join, split, and inspect itself, replacing many scattered os.path functions.

Creating a Path

Import Path and construct one from a string. Printing it shows the path text.

from pathlib import Path

p = Path('folder/file.txt')
print(p)

Joining with the / Operator

The slash operator joins path parts. It is far more readable than string concatenation and handles separators correctly on every OS.

from pathlib import Path

base = Path('data')
full = base / 'reports' / 'march.txt'
print(full)

Name, Stem, and Suffix

A path exposes its parts as properties: name is the final component, stem drops the extension, and suffix is the extension.

from pathlib import Path

p = Path('data/report.csv')
print(p.name)
print(p.stem)
print(p.suffix)

Parent Directory

The parent property gives the containing directory as another Path. Chain it to climb upward.

from pathlib import Path

p = Path('a/b/c/file.txt')
print(p.parent)
print(p.parent.parent)

All Parts

The parts property returns a tuple of every component, which is handy for iterating over a path's pieces.

from pathlib import Path

p = Path('usr/local/bin')
print(p.parts)

Changing the Extension

with_suffix() returns a new path with a different extension, and with_name() replaces the whole final component.

from pathlib import Path

p = Path('report.txt')
print(p.with_suffix('.md'))
print(p.with_name('summary.txt'))

Current and Home Directories

Class methods Path.cwd() and Path.home() give the current working directory and the user's home directory.

from pathlib import Path

print(type(Path.cwd()))
print(type(Path.home()))

Checking Existence and Type

Query the filesystem with exists(), is_file(), and is_dir(). They return booleans without raising on a missing path.

from pathlib import Path

p = Path('definitely_not_here_12345.txt')
print(p.exists())
print(p.is_file())
print(p.is_dir())

Paths Are Strings When Needed

Most file functions accept Path objects directly. When you truly need a string, wrap it in str().

from pathlib import Path

p = Path('data') / 'file.txt'
text = str(p)
print(text)
print(type(text))

Cross-Platform by Design

A Path automatically uses the right separator for the operating system you run on. You write the same code with / joins, and it produces correct paths on Linux, macOS, and Windows.

from pathlib import Path

p = Path('config') / 'settings' / 'app.ini'
print(p)
print(p.name)

Quick Check

What does the / operator do with Path objects?

Recap

You learned to build paths the modern way.

  • Path represents a filesystem path as an object.
  • Join with /; inspect with name, stem, suffix, parent, parts.
  • with_suffix and with_name derive new paths.
  • Check the filesystem with exists, is_file, is_dir.

Frequently asked questions

Is the “The Path Object” lesson free?

Yes — the full text of “The Path Object” 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 “The Path Object”?

Build paths the modern way. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Path Object” 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. The Path Object
  2. Reading and Writing Files
  3. Globbing and Iterating
  4. Path Manipulation
← Back to Python Academy