Python Glossary Русский

Python 3.4+pathlibterm

pathlib.PurePath.parent

path components

The path without its last component — the logical parent: '/data' for '/data/a.txt'.

Syntax

PurePath(...).parent

parent works on the path string, not the disk: the parent of '../x' is '..' and of 'a/..' is 'a', with no '..' resolution. For the real parent call resolve() first. All ancestors in order are in the parents property.

Examples

from pathlib import PurePosixPath as P
p = P('/data/2024/report.txt')
print(p.parent)  # → /data/2024
print(p.parent.parent)  # → /data
print(list(p.parents))  # → [PurePosixPath('/data/2024'), PurePosixPath('/data'), PurePosixPath('/')]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy