pathlib Table of Contents Overview Overview from pathlib import Path p = Path('/path/to/file') print(p.parents[0]) # /path/to print(p.parents[1]) # /path p.mkdir(parents=True, exist_ok=True) https://docs.python.org/3/library/pathlib.html