Path Manipulation
Join, resolve, and inspect paths.
Path Manipulation 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.
Inspecting and Transforming Paths
Beyond building paths, pathlib lets you resolve them to absolute form, test relationships between them, and read filesystem metadata, all without touching os.path.
Absolute vs Relative
is_absolute() reports whether a path is anchored at the root. Relative paths are interpreted against the current directory.
from pathlib import Path
print(Path('docs/file.txt').is_absolute())
print(Path('/usr/local').is_absolute())Resolving a Path
resolve() turns a path into an absolute path and collapses . and .. segments. It is the go-to for normalizing paths.
from pathlib import Path
p = Path('a/b/../c/file.txt')
print(p.resolve())Joining with joinpath
Besides the / operator, joinpath() appends one or more components. It is handy when the parts come from a list.
from pathlib import Path
base = Path('project')
parts = ['src', 'main', 'app.py']
print(base.joinpath(*parts))Relative Paths Between Two
relative_to() computes the path of one location relative to another. It raises if the path is not under the given base.
from pathlib import Path
full = Path('/home/user/docs/report.txt')
rel = full.relative_to('/home/user')
print(rel)Testing a Prefix with is_relative_to
is_relative_to() safely checks whether one path lies under another, returning a boolean instead of raising.
from pathlib import Path
p = Path('/home/user/docs/file.txt')
print(p.is_relative_to('/home/user'))
print(p.is_relative_to('/etc'))Multiple Suffixes
For names like archive.tar.gz, suffixes lists every extension, while suffix gives only the last.
from pathlib import Path
p = Path('archive.tar.gz')
print(p.suffix)
print(p.suffixes)Anchor and Drive
The anchor property is the root part of the path. On POSIX it is typically /; on Windows it includes the drive letter.
from pathlib import Path
p = Path('/usr/local/bin')
print('anchor:', p.anchor)
print('parts:', p.parts)Reading File Metadata
stat() returns filesystem metadata such as the size in bytes. Here we create a file and read its size.
from pathlib import Path
p = Path('meta.txt')
p.write_text('hello world')
print('size:', p.stat().st_size, 'bytes')Renaming and Moving
rename() changes a path's location or name. Combined with the building methods, it makes file reorganization concise.
from pathlib import Path
old = Path('old_name.txt')
old.write_text('data')
new = old.with_name('new_name.txt')
old.rename(new)
print(new.exists(), new.read_text())Expanding the Home Shortcut
expanduser() replaces a leading ~ with the user's home directory, mirroring how the shell expands it.
from pathlib import Path
p = Path('~/configs/app.ini')
expanded = p.expanduser()
print(expanded.is_absolute())Quick Check
What does the resolve() method do to a path?
Recap
You learned to join, resolve, and inspect paths.
resolvemakes an absolute, normalized path.relative_toandis_relative_tocompare locations.suffix,suffixes,anchor,partsreveal structure.statreads metadata;renamemoves files.
Frequently asked questions
Is the “Path Manipulation” lesson free?
Yes — the full text of “Path Manipulation” 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 “Path Manipulation”?
Join, resolve, and inspect paths. 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 “Path Manipulation” 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
- The Path Object
- Reading and Writing Files
- Globbing and Iterating
- Path Manipulation