Skip to main content

Superpathlib

PyPI version PyPI downloads Python version Operating system Coverage

Maximize your productivity with minimal code!

Superpathlib is an enhanced version of Python's standard pathlib, designed to make file operations more intuitive and extend functionality without compromising performance. It builds on pathlib by introducing additional properties, methods, and shortcuts that simplify common file manipulations and reduce boilerplate code.

For custom operations, the package supports subclassing, which has been added to the standard library in python3.12.

Key features:

  • Read and write content in various formats such as text, bytes, YAML, JSON, and even NumPy arrays
  • Access and modify file metadata like file size and modification time
  • Utility methods:
    • rmtree to remove directories recursively
    • copy_to to copy content
    • tempfile to create and use temporary files
    • find for recursive search
    • unpack_if_archive for unpacking archives

Usage

from superpathlib import Path

path = Path(filename)

1) Use properties to read & write path content in different formats

  • text
  • byte_content
  • lines
  • yaml
  • json
  • numpy

read-only:

  • cached_yaml: yaml content, cached as json next to the file

gpg-encrypted on disk:

  • encrypted: prefix any of the above to read & write plaintext (e.g. path.encrypted.json)

examples:

path.json = {key: value}
path.encrypted.json = {key: secret}

for line in path.lines:
    if interesting(line):
        process(line)

2) Use instance properties to get/set file metadata:

  • get:
    • size: filesize
    • is_root: whether the owner of the file is a root user
    • has_children: whether a path has children
    • number_of_children: number of children in a folder
    • filetype: content type of a file
    • content_hash: a hash of the complete substructure found in a folder
  • get & set:
    • mtime: modified time

examples:

path_new.mtime = path_old.mtime

if path.filetype == "video":
    process(path)

3) Use class properties to access commonly used folders:

  • docs
  • assets
  • ..

example:

names_path = Path.assets / 'names'
names = names_path.lines

4) Use additional functionality

  • find(): recursively find all paths under a root that match a condition (extra options available for performance optimization)
  • rmtree(): remove directory recursively
  • copy_to(dest): copy content to dest
  • copy_mtime_to(dest): recursively copy modified time to all n-level children of dest
  • tempfile(): create temporary file that can be used as context manager
  • unpack_if_archive(): extract an archive(zip, tar, ..) file to desired folder, recursively
  • pop_parent(): remove first parent from path in filesystem

examples:

with Path.tempfile() as tmp:
    do_work(logfile=tmp)
    log = tmp.text
process(log)

is_git_root = lambda p: (p / '.git').exists()
for git_path in root.find(is_git_root):
    process_git(git_path)

5) Enhance existing functionality

  • Automatically create parents when writing files, creating new files, renaming files, ..
  • Return default values when path does not exist (e.g. size = 0, lines=[])
  • Support replacing folders instead files only if specified

6) Inherit from superpathlib Path to define your own additional functionality:

example:

import superpathlib

class Path(superpathlib.Path):
    def count_children(self):
        return sum(1 for _ in self.iterdir())

The standard pathlib library supports this for Python versions starting from python3.12.

Installation

pip install superpathlib

or

pip install superpathlib[full]

to include additional dependencies like PyYAML for handling YAML files

or

Manually install the packages corresponding with the features you want to use:

  • Packages listed in pyproject.toml

Metadata

Release files for superpathlib 2.1.2

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

Source distribution (sdist)

Source distribution for superpathlib 2.1.2
File Size Uploaded
superpathlib-2.1.2.tar.gz 16.9 kB Details

Built distribution (wheel)

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

Total release size: 31.3 kB

Release files / superpathlib-2.1.2.tar.gz

Download URL superpathlib-2.1.2.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a611684f574accd01258c41917005568bf5665a14f218cca9ea8c082bcdb1a45
BLAKE2b-256 checksum
How to use checksums
72dc10854bddd08e6e14623423d3342fa5b33b169b43a89edf89015ced81306d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / superpathlib-2.1.2-py3-none-any.whl

Download URL superpathlib-2.1.2-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f06f7893a78ab16ea60095bd790885bc85dfee966b6d6bb0e4107c471332b98
BLAKE2b-256 checksum
How to use checksums
60273ac7993895c758d938b602155a855cfd1ecf1b840895bd00c626847c7fc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

2.1.2 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.14

2 release files

2.0.13

2 release files

2.0.11

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.3.13

2 release files

1.3.12

2 release files

1.3.11

2 release files

1.3.10

2 release files

1.3.9

2 release files

1.3.8

2 release files

1.3.7

2 release files

1.3.6

2 release files

1.3.5

2 release files

1.3.4

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.12

2 release files

1.1.11

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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