Python Pathlib Quickstart: Paths, Folders and Files
pathlib is part of the Python standard library and replaces os.path string juggling with Path objects. Import Path, start from Path.cwd(), Path.home() or Path(__file__), join folders with the / operator, create folders with mkdir(), read .name, .stem and .suffix, and loop over files with iterdir(), glob() or rglob().
What pathlib is
pathlib is a module in the Python standard library, available since Python 3.4, that represents file system paths as objects instead of plain strings. You do not install anything. A Path object knows its name, its extension and its parent folder, and it works the same way on Windows, Mac and Linux.
Before pathlib, you joined paths with os.path.join and split them with string functions. With pathlib, the same work reads like plain English. I use it in almost every automation script on this blog, for example to loop through folders and subfolders or to combine all Excel files in a folder.
All you need is one import:
from pathlib import Path
Current, home and script folder
Path.cwd() returns the folder you run Python from, Path.home() returns your user folder, and Path(__file__) returns the path of the running script. In a script, Path(__file__).parent is the safest starting point, because it does not change when you run the script from another folder.
home_dir = Path.home()
cwd = Path.cwd()
# Path of the running .py file (does not work in Jupyter Notebooks)
curr_file = Path(__file__)
script_dir = Path(__file__).parent
__file__ does not exist in a Jupyter Notebook, so use Path.cwd() there. The notebook’s working directory is usually the folder of the notebook.
The parent folder
Use .parent for the folder one level up and .parents with an index for higher levels. parents[0] is the direct parent, parents[1] the folder above that, and so on.
one_above = Path.cwd().parent
two_above = Path.cwd().parents[1]
Join paths
Use the slash operator between a Path and a string. pathlib inserts the right separator for your operating system, so the same code works on Windows and Mac.
joined_path = cwd / "Output"
report = cwd / "Output" / "report.xlsx"
Create a folder
Call mkdir() on the path. exist_ok=True stops the FileExistsError when the folder is already there, and parents=True also creates any missing folders above it.
joined_path.mkdir(parents=True, exist_ok=True)
File or folder
is_dir() and is_file() return True or False, and exists() checks whether anything is there at all. They are handy before you open a file or write to a folder.
joined_path.is_dir()
example_file = cwd / "somefile.txt"
example_file.is_file()
example_file.exists()
File name, stem and extension
A Path splits its last part for you: .name is the full file name, .stem the name without extension and .suffix the extension including the dot.
| Attribute | Result for somefile.txt |
|---|---|
example_file.name |
somefile.txt |
example_file.stem |
somefile |
example_file.suffix |
.txt |
example_file.parent |
the folder that holds the file |
I use .stem all the time to name output files after their source, for example output_dir / f"{file.stem}_New.xlsx" when I find and replace values in multiple Excel files.
Loop over the files in a folder
iterdir() returns everything directly inside a folder, glob() filters by a pattern, and rglob() searches all subfolders too. Each result is again a Path, so you can use .suffix or .name inside the loop.
target_dir = cwd / "Sample Files"
# Everything in the folder
for file in target_dir.iterdir():
print(file)
# Only Excel files
for file in target_dir.iterdir():
if file.suffix == ".xlsx":
print(file)
# The same with a pattern
for file in target_dir.glob("*.xlsx"):
print(file)
# Everything, including subfolders
for file in target_dir.rglob("*"):
print(file)
I tested all snippets on this page with Python 3.12 in November 2026. The notebook and a printable cheat sheet are in the GitHub repo.
pathlib or os.path
For new code, use pathlib. It is shorter, easier to read, and every function that accepts a file name, like open() or pandas read_excel(), also accepts a Path. os.path still works and you will see it in older tutorials, so it helps to recognize it.
| Task | pathlib |
|---|---|
| Join paths | Path(a) / "b"instead of os.path.join(a, "b") |
| Current folder | Path.cwd()instead of os.getcwd() |
| File name | Path(p).nameinstead of os.path.basename(p) |
| Extension | Path(p).suffixinstead of os.path.splitext(p)[1] |
| Create folder | Path(p).mkdir(parents=True, exist_ok=True)instead of os.makedirs(p, exist_ok=True) |
A good next step is a real task: copy data from multiple Excel files into a master file uses Path and glob exactly like this.
Importing the main class:
from pathlib import Path
Get the home directory
home_dir = Path.home()
Get the path to the current working directory
cwd = Path.cwd()
Get the path to the current Python file (does not work in Jupyter Notebooks)
curr_file = Path(__file__)
Get the first parent folder path
one_above = Path.cwd().parent
Get the Nth parent folder path
mul_above = Path.cwd().parents[0]
Join paths
joined_path = cwd / 'Output'
Create a directory if it does not exist
joined_path.mkdir(exist_ok=True) # exist_ok: to ignore 'FileExistsError'if the target directory already exists
Check if the path is a folder
joined_path.is_dir()
Check if the path is a file
example_file = cwd / "somefile.txt"
Get the file name
file_name = example_file.name
Get the file name w/o extension
file_name = example_file.stem
Get the file extension
file_extension = example_file.suffix
Iterate over files in a directory
target_dir = cwd / "Sample Files"
for file in target_dir.iterdir():
print(file)Iterate over files in a directory combined with suffix
for file in target_dir.iterdir():
if file.suffix == ".xlsx":
print(file)Iterate over files in a directory incl. sub folder(s)
for file in target_dir.rglob("*"):
print(file)