Sub topic 4
Taking paths apart: name, stem, suffix, parent, parts
This chapter assumes you can create a Path object and join segments with the / operator. It also assumes you know basic Python syntax and what files and directories are.
A Path object exposes its pieces as attributes. You read them directly, and no file needs to exist. These attributes only parse the text of the path.
The main attributes
Here is one path with several extensions. It shows what each attribute returns.
from pathlib import Path
p = Path("/home/ana/archive/report.final.tar.gz")
print(p.name) # report.final.tar.gz
print(p.stem) # report.final.tar
print(p.suffix) # .gz
print(p.suffixes) # ['.final', '.tar', '.gz']
print(p.parent) # /home/ana/archive
print(p.parts) # ('/', 'home', 'ana', 'archive', 'report.final.tar.gz')| Attribute | Meaning | Type |
|---|---|---|
| `name` | Final component, including the extension | `str` |
| `stem` | `name` without the last suffix | `str` |
| `suffix` | Last extension, with its leading dot | `str` |
| `suffixes` | Every extension, in order | `list` of `str` |
| `parent` | Everything before the final component | `Path` |
| `parts` | Each component, including the root | `tuple` of `str` |
How stem and suffix split
suffix and stem split only at the last dot. For report.final.tar.gz, the suffix is .gz and the stem is report.final.tar. To get every extension, use suffixes.
Some names have no suffix. A name that starts with a dot, such as .bashrc, is treated as having no suffix, because the leading dot is part of the name.
from pathlib import Path
print(repr(Path(".bashrc").suffix)) # ''
print(Path(".bashrc").stem) # .bashrc
print(repr(Path("Makefile").suffix)) # ''
print(Path("archive.tar.gz").suffixes)Walking up with parent
parent returns a Path, so you can chain it to move up the tree. The parents attribute gives a sequence of all ancestors, nearest first.
from pathlib import Path
p = Path("/srv/app/logs/app.log")
print(p.parent) # /srv/app/logs
print(p.parent.parent) # /srv/app
for ancestor in p.parents:
print(ancestor)
# /srv/app/logs
# /srv/app
# /srv
# /A relative path ends at ., which represents the current directory. For example, Path("notes/draft.txt").parent is notes, and Path("draft.txt").parent is ..
Looking at parts
parts is useful when you need to inspect individual directories. An absolute POSIX path starts with '/' as the first element. A relative path has no root element.
from pathlib import Path
print(Path("data/reports/2026/summary.csv").parts)
# ('data', 'reports', '2026', 'summary.csv')
if "reports" in Path("data/reports/2026/summary.csv").parts:
print("inside a reports folder")Checking parts is safer than searching the string with in. A string test for "report" would also match reports and report.final.tar.gz. A tuple test matches whole components only.
Common errors
- Treating
suffixas the full extension. For.tar.gzfiles it returns only.gz. Usesuffixesor checkname.endswith(".tar.gz"). - Comparing
suffixwithout regard to case.Path("PHOTO.JPG").suffixis.JPG, so call.lower()first. - Expecting
parentto be a string. It is aPath, so usestr()oros.fspath()when an API needs text. - Assuming
parts[0]is a directory name. On an absolute path it is the root.
Exercise
Given p = Path("/data/images/photo.backup.jpeg"), what do p.stem, p.suffix, and p.parent.name return?
Show answer
p.stem is photo.backup, p.suffix is .jpeg, and p.parent.name is images. The stem and suffix split at the last dot only. p.parent is /data/images, and its name is the final component.
Exercise
Write a short check that is true when a path has the extension .tar.gz, whatever the case of the letters.
Show answer
"".join(p.suffixes).lower() == ".tar.gz" works for archive.tar.gz. For a name like report.final.tar.gz it would give .final.tar.gz, so p.name.lower().endswith(".tar.gz") is the more reliable check.