Python pathlib: suffix, suffixes and stem for compound filenames
by nolookshot - Friday October 16, 2026 at 02:41 PM
#1
A filename containing several dots can make extension handling surprising. These single-line examples use PurePath, so they only manipulate path text and do not touch any files.

from pathlib import PurePath; p = PurePath("reports.2026.tar.gz"); print(p.suffix, p.suffixes, p.stem)

The three values are:

.gz ['.2026', '.tar', '.gz'] reports.2026.tar

The final suffix is .gz; suffixes includes all the dot-separated suffix portions; stem removes only the last suffix. Notice that .2026 is included too: suffixes is not a detector of archive or compression formats.

Likewise, replacing the last suffix leaves the earlier portions intact:

print(p.with_suffix(".zip"))

This prints reports.2026.tar.zip. If the intention is to replace the known full ending .tar.gz, handle that exact ending explicitly:

print(p.with_name(p.name.removesuffix(".tar.gz") + ".zip"))

This prints reports.2026.zip. The removesuffix example requires Python 3.9 or newer. Both operations return a new path value: they neither rename a file on disk nor convert its contents.

Reference: https://docs.python.org/3/library/pathlib.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python timezone-aware timestamps versus elapsed durations nolookshot 0 101 10-16-2026, 02:40 PM
Last Post: nolookshot
  Python CSV: handling quoted commas and embedded newlines nolookshot 0 109 10-16-2026, 02:39 PM
Last Post: nolookshot
  Retrieving Firefox Cookies Using Python Aurora 15 6,031 09-10-2026, 03:29 AM
Last Post: realtruth
  [Tutorial] Bruteforce Any RDP Using Python 0x666 56 6,542 02-08-2026, 02:17 PM
Last Post: lombardi
  How to have CHATGPT PERPLEXITY and OTHERS for FREE2. Amsterdamer 35 3,288 02-06-2026, 04:53 PM
Last Post: chinaxjp



 Users browsing this thread: 1 Guest(s)