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
Creating a Path
from pathlib import Path
p = Path('data/file.txt')
print(p)Path Joining with /
from pathlib import Path
p = Path('data') / 'sub' / 'file.txt'
print(p)Path Properties
from pathlib import Path
p = Path('data/report.csv')
print(p.name, p.stem, p.suffix, p.parent)Checking Existence
from pathlib import Path
p = Path('/tmp')
print(p.exists(), p.is_dir(), p.is_file())Reading and Writing
from pathlib import Path
p = Path('/tmp/test_pathlib.txt')
p.write_text('hello pathlib')
print(p.read_text())Iterating Directory Contents
from pathlib import Path
for child in Path('/tmp').iterdir():
if child.suffix == '.txt':
print(child.name)
breakRecursive Glob
from pathlib import Path
for f in Path('/tmp').rglob('*.txt'):
print(f.name)
breakCreating Directories
from pathlib import Path
Path('/tmp/test_dir/sub').mkdir(parents=True, exist_ok=True)
print('created')Renaming and Deleting
from pathlib import Path
p = Path('/tmp/test_pathlib.txt')
if p.exists():
p.unlink()
print('deleted')Absolute and Relative Paths
from pathlib import Path
print(Path('.').resolve())Quick Check
Recap
Keep Going
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
- Opening and Reading Files
- Writing and Appending to Files
- Using the with Statement for Files
- Working with File Paths using pathlib