Skip to main content

A collection of functions working with `pathlib.Path`

Project description

pathext

pathext is a collection of functions working with pathlib.Path. They're often wrappers for standard library functionality with some extensions.

Executable lookup

pathext.which()

Wrapper for shutil.which() which returns the result as an absolute Path (or None if it fails to find the executable). It also has a couple extra features, see below.

Arguments (all of them except name are optional):

  • name: str - Executable name to look up.
  • path: None | str | Sequence[Path] - Directory list to look up name in. If set to None, or set to a string, then it is passed to shutil.which() as-is. If set to a list, concatenates the list items using os.pathsep, and passes the result to shutil.which(). Defaults to None. See shutil.which()'s documentation on exact behaviour of this argument.
  • cwd: Optional[Path] - If specified, then changes the current working directory to cwd for the duration of the shutil.which() call. Note that since it is changing global state (the current working directory), it is inherently not thread-safe.

pathext.checked_which()

Same as pathext.which(), except it raises ValueError instead of returning None if it cannot find the executable.

Manipulating PATH strings

pathext.split_path_list()

Split PATH string based on os.pathsep and convert each component to pathlib.Path.

Empty components will be removed, i.e. leading, trailing or duplicated separators will not cause issues.

In contrast to str.split(), if the string is empty, the function will return an empty list.

pathext.join_path_list()

Create PATH string (os.pathsep-separated string) from list of paths. The list is allowed to contain Path objects, strings and even None. Empty strings and Nones will be removed before joining the list.

pathext.deduplicate_path_list()

Create PATH string (os.pathsep-separated string) from list of paths. The list is allowed to contain Path objects, strings and even None. Empty strings and Nones will be removed before joining the list.

Generic utilities

pathext.to_path()

Simple function that converts a str to a Path (just like Path's constructor), but also handles None by returning None. It can be used to convert the return value of functions that return str | None to Path | None.

pathext.temporary_directory()

Context manager similar to tempfile.TemporaryDirectory except it returns the created directory's name as an absolute pathlib.Path instead of str.

Supports all arguments of tempfile.TemporaryDirectory, including delete introduced in Python 3.12. If delete is specified on Python 3.11 or older, ValueError is raised.

Note that the returned path is always absolute, even if the dir parameter is relative. This is consistent with how tempfile.TemporaryDirectory works starting with Python 3.12.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathext-1.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pathext-1.4-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pathext-1.4.tar.gz.

File metadata

  • Download URL: pathext-1.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for pathext-1.4.tar.gz
Algorithm Hash digest
SHA256 e4e7ada05ac3a4b1c74a8784f88362f9c8fd480d38725baf76a6ef7cf3dcd5fe
MD5 f8008eab0da173ad83d55fdd52326322
BLAKE2b-256 13f265e9d939888cbe0ff536f96fc7f8c3da7d433250f5c3d6c640675eaff3a0

See more details on using hashes here.

File details

Details for the file pathext-1.4-py3-none-any.whl.

File metadata

  • Download URL: pathext-1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for pathext-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 356f1a3e2e543c20cb40458037d027d29e351166a23620a85432739ae65e74a6
MD5 03ac1eb4130a8415218eca903f42508c
BLAKE2b-256 269de99e0d060e97373ad3a8be63566f3cd13fb1b224ed3f50f434682f19ae47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page