Skip to main content

shortenpath

Lianes8 Certified

Shorten a filesystem path to fit a terminal column budget for prompts, status bars, tables and logs. The home folder folds to ~, and whole folders in the middle give way to .... The root and file name are retained when the budget permits; smaller budgets can drop the root or cut the file name. Display only: the filesystem is never read.

Install

pip install shortenpath

Usage

from shortenpath import shorten

path = "/home/javi/projects/shop/src/cart/checkout.py"
print(shorten(path, 60, flavor="posix", home="/home/javi"))
print(shorten(path, 30, flavor="posix", home="/home/javi"))
print(shorten(path, 22, flavor="posix", home="/home/javi"))
print(shorten(path, 14, flavor="posix", home="/home/javi"))
print(shorten(path, 9, flavor="posix", home="/home/javi"))
# ~/projects/shop/src/cart/checkout.py
# ~/.../src/cart/checkout.py
# ~/.../cart/checkout.py
# checkout.py
# ch...t.py

Windows paths prefer to keep their drive, UNC share or device prefix whole when it fits, and both \ and / separate folders (each kept as written). The home folder matches regardless of case. A path consisting only of an anchor can have that anchor cut.

from shortenpath import shorten

print(shorten(r"C:\Users\javi\AppData\Local\Temp\report.pdf", 24, flavor="windows"))
print(shorten(r"\\fileserver\finance\2026\q3\budget.xlsx", 36, flavor="windows"))
print(shorten(r"\\?\C:\very\deep\tree\of\folders\notes.txt", 26, flavor="windows"))
print(
    shorten("C:/Users/javi/Downloads/tools/setup.exe", 26, flavor="windows", home=r"c:\users\javi")
)
# C:\Users\...\report.pdf
# \\fileserver\finance\...\budget.xlsx
# \\?\C:\very\...\notes.txt
# ~/Downloads/.../setup.exe

The budget is in terminal columns: East Asian wide characters take two, combining accents none, and a cut never splits a letter from its accent.

from shortenpath import display_width, shorten

path = "/srv/写真/2026/夏休み/海辺の夕焼け.jpg"
print(display_width(path), len(path))
print(shorten(path, 30, flavor="posix"))
print(shorten(path, 24, flavor="posix"))
# 38 27
# /srv/写真/.../海辺の夕焼け.jpg
# /.../海辺の夕焼け.jpg

Any separator and any placeholder:

from shortenpath import shorten

print(shorten("docs::api::v2::internal::client", 20, sep="::"))
print(shorten("/var/log/nginx/access.log", 16, flavor="posix", placeholder="…"))
# docs::...::client
# /…/access.log

The rules

shorten(path, width, *, flavor="native", home_tilde=True, home=None, sep=None, placeholder="...") returns path itself when it fits, otherwise the first of these that fits width columns:

  1. The anchor, then as many whole folders as fit around the placeholder, then the last name: the form keeping the most names wins, and between equals the one keeping more at the start. The anchor is a POSIX root (/, //), a drive (C:\, C:), a UNC share (\\server\share\), a device path (\\.\C:\, \\?\Volume{...}\, \\?\UNC\server\share\) or ~.
  2. The placeholder, the separator and the last name: the whole name is worth more than the anchor. A run of separators that does not fit shrinks to one.
  3. The last name alone, then the anchor with the name cut in the middle (a path with a single name and room for both).
  4. The last name cut in the middle: its start, the placeholder, and its end, which keeps the extension and the stem character before it when there is room for them. With no room for the placeholder only the start of the name is kept, and when not even its first character fits the result is empty.

An anchor-only path is cut using the same middle-cut and prefix fallbacks. For example, shorten("C:\\", 1, flavor="windows", home_tilde=False) returns "C".

Option Meaning
flavor "posix", "windows" or "native" (the running system). Flavor is its type.
home_tilde, home Fold the home folder to ~. home defaults to the current user's for native paths. A relative home, or one that is only a root, never folds.
sep A custom separator instead of the flavor's rules, for URLs or keys.
placeholder What stands for the removed folders, ... by default.

Separator runs are kept as written. On Windows a path that starts exactly with \\?\ skips normalization, so there only \ separates and / is part of a name. display_width(text) gives the columns of any text: East Asian wide and fullwidth characters and characters with the emoji presentation selector take two, combining marks, format characters and Hangul medial and final jamo none, everything else (control characters included) one, with soft hyphen counted as one. Emoji joined into one picture are counted as their parts. This is a width convention, not terminal emulation or full grapheme segmentation. Python's Unicode database determines character widths. A selector at a joined piece boundary can require a final prefix cut to enforce the budget. Wrong types raise TypeError; a width below one, an empty separator or placeholder, or an unknown flavor raises ValueError. Error messages do not quote input values.

Compared with the Win32 PathCompactPathEx function, this removes whole middle folders, prefers a whole anchor and last name, and preserves the extension of a cut name when the extension and required stem characters fit. It counts columns instead of characters and returns a path exactly as long as the budget unchanged. Small budgets can drop the anchor or cut the extension; an anchor-only path can itself be cut.

Command line

$ shortenpath -w 24 -f posix --home /home/javi /home/javi/projects/shop/src/cart/checkout.py
~/.../cart/checkout.py
$ shortenpath -w 24 -f windows 'C:\Program Files\Common Files\system\ado\msado15.dll'
C:\...\ado\msado15.dll

Paths come from the arguments, or one per line from standard input, each written as soon as it is read. -w defaults to the terminal width. Bad input exits with status 2. python -m shortenpath works too.

Why

Python's os.path.expanduser expands ~ to a home directory. textwrap.shorten collapses whitespace and drops words from the end. Those are different operations from keeping recognizable folders and the filename within a display budget. shortenpath handles Windows and POSIX path forms on either system, folds the home folder, and has no runtime dependencies. Use a terminal-aware renderer when escape sequences or exact emoji layout matter; this library preserves control characters and does not sanitize them.

The GUIDE details the contract and limitations. The CHANGELOG records release changes.

Development

python scripts/fetch_sources.py downloads the Microsoft, POSIX and Unicode texts the tests check the rules against (listed with their hashes in tests/sources/MANIFEST.txt); without them those tests skip. Set SHORTENPATH_SOURCES to use a different download folder. SHORTENPATH_E2E=1 pytest builds the wheel and tests the installed package; additionally set SHORTENPATH_PYPI=1 to exercise the upgrade from PyPI 0.2.0. scripts/check.ps1 runs the whole gate. Install .[dev] first. Tests have a 300-second timeout; pass --basetemp to choose their scratch folder.

License

MIT. See LICENSE.

Metadata

Release files for shortenpath 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shortenpath 1.0.0
File Size Uploaded Provenance Actions
shortenpath-1.0.0.tar.gz 43.4 kB Without provenance Details for shortenpath-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shortenpath 1.0.0
File Interpreter ABI Platform Actions
shortenpath-1.0.0-py3-none-any.whl Python 3 none any Details for shortenpath-1.0.0-py3-none-any.whl

Total release size: 59.9 kB

Release files / shortenpath-1.0.0.tar.gz

Download URL shortenpath-1.0.0.tar.gz
Size 43.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c33586461086374b3444de3218de4aad4d97a8b48fa72df1ec691f24ce62931c
BLAKE2b-256 checksum
How to use checksums
5278c8792deb9adbb9b877a4cb866df8a2b2e9e2b9ba883c82497b5bbddc3387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / shortenpath-1.0.0-py3-none-any.whl

Download URL shortenpath-1.0.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cec2c7991964c4f9fb9dc0f63b06e0a5c723a5aef475bbe058941c26f0338853
BLAKE2b-256 checksum
How to use checksums
86738b5da51345760107b29dfae39793445cb98b40d3b647e98711da069df008
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for shortenpath 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page