Skip to main content

How to Change the File Name in a Path in Python with with_name

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<path>.with_name(<new name>)
PartWhat 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​

with_name_demo.py
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)

The with_name_demo.py script in a macOS-style code window: Path reports/2026/sales.csv, then print of report.with_name("summary.csv"), report.with_name(report.name + ".tmp") and report.

  • report.with_name("summary.csv") keeps the folders and swaps the file name.
  • report.name is the current file name, sales.csv, so report.name + ".tmp" is sales.csv.tmp.
  • The last line prints report to show it didn't change.

Step 2: Run It​

Terminal
uv run python with_name_demo.py
Output
reports/2026/summary.csv
reports/2026/sales.csv.tmp
reports/2026/sales.csv

The terminal after uv run python with_name_demo.py: reports/2026/summary.csv, reports/2026/sales.csv.tmp, then the unchanged 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​

MistakeWhat happensDo 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 nameUse it on a path that ends in a file or folder name
Expecting it to rename the fileThe file keeps its name: with_name() only builds a pathRename on disk with report.rename(...) or report.replace(...)

Troubleshooting​

SymptomLikely causeFix
ValueError: Invalid name ...The new name holds a folder separator, or is emptyPass a bare file name
The new file isn't on diskwith_name() doesn't create or rename anythingWrite 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​