Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pathlib parents (dirname)

  • parent
  • patents
  • dirname
from pathlib import Path

this = Path(__file__)
print(this)
print(this.parent)       # dirname
print(this.parents[0])   # dirname (first parent)
print(this.parents[1])   # grandparent
...
print(this.parents[-1])  # /