How to Change the File Name in a Path in Python with with_name
Often you need a second file in the same folder as one you already have a path to: a summary next to a report, or a temporary file next to the real one. pathlib's with_name() gives you that path by swapping only the last part. This guide builds two such paths on Python 3.15.2.
Syntax
<path>.with_name(<new name>)
| Part | What it means |
|---|---|
<path> | A pathlib.Path, such as Path("reports/2026/sales.csv") |
<new name> | The new last part: a file name such as "summary.csv", with no folder in it |
The pathlib documentation says with_name() will "Return a new path with the name changed. If the original path doesn't have a name, ValueError is raised". It only builds a path: nothing on disk is renamed.
The Example
The script starts from the path reports/2026/sales.csv, makes a path to summary.csv in the same folder, makes one to sales.csv.tmp by adding to the current name, and prints the original path again.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
from pathlib import Path
report = Path("reports/2026/sales.csv")
print(report.with_name("summary.csv"))
print(report.with_name(report.name + ".tmp"))
print(report)

report.with_name("summary.csv")keeps the folders and swaps the file name.report.nameis the current file name,sales.csv, soreport.name + ".tmp"issales.csv.tmp.- The last line prints
reportto show it didn't change.
Step 2: Run It
uv run python with_name_demo.py
reports/2026/summary.csv
reports/2026/sales.csv.tmp
reports/2026/sales.csv

Both new paths point into the same reports/2026 folder, and report is still sales.csv: with_name() returned new paths. The folders don't have to exist, because nothing touched the disk. The command works unchanged in PowerShell. On Windows, the paths print with backslashes, such as reports\2026\summary.csv.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Putting a folder in the new name: with_name("old/sales.csv") | ValueError: Invalid name 'old/sales.csv' | Build the path with / instead: report.parent / "old" / "sales.csv" |
Calling it on a path that has no name, such as Path("/") | ValueError: PosixPath('/') has an empty name | Use it on a path that ends in a file or folder name |
| Expecting it to rename the file | The file keeps its name: with_name() only builds a path | Rename on disk with report.rename(...) or report.replace(...) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError: Invalid name ... | The new name holds a folder separator, or is empty | Pass a bare file name |
| The new file isn't on disk | with_name() doesn't create or rename anything | Write to the new path, or rename the old file to it |
FAQ
Does with_name() rename the file?
No. With a real sales.csv on disk, Path("sales.csv").with_name("summary.csv") leaves sales.csv where it is, and summary.csv doesn't exist until you write to it or rename something to it.
How do I change only the extension?
Use with_suffix(): Path("reports/2026/sales.csv").with_suffix(".json") gives reports/2026/sales.json.
What does it look like on Windows?
The same, with backslashes. PureWindowsPath, which builds Windows paths on any computer, shows it: PureWindowsPath("reports/2026/sales.csv").with_name("summary.csv") prints reports\2026\summary.csv.
Conclusion
report.with_name("summary.csv") and report.with_name(report.name + ".tmp") gave two new paths in the same folder, and report itself stayed the same. Pass a bare file name, and remember that nothing on disk changes until you write or rename.
Additional Resources
- PurePath.with_name() — the method.
- PurePath.with_suffix() — change only the extension.
