Skip to main content

betterpath, or “bp”, is an adaptation of the classic Twisted FilePath type and interface. bp provides a simple, robust, well-tested object abstraction over file paths, generalizing the concept of file paths beyond filesystems.

File Paths

bp exposes an interface, bp.abstract.IFilePath, for file paths, and provides the following concrete implementations:

  • bp.filepath.FilePath, for the root filesystem

  • bp.zippath.ZipPath, for ZIP archives

  • bp.memory.MemoryPath, for in-memory temporary filesystems

In addition, there are combining abstract file paths which can be wrapped around other file paths:

  • bp.readonly.ReadOnlyPath, for read-only filesystems

API documentation is available at http://betterpath.rtfd.org/.

Vs. the Competition

os.path

The venerable champ, os.path has been the cause of (and solution to) most of Python’s path problems over the years.

Pros

  • In the standard library

Cons

  • Unsafe

  • Verbose

  • Operates on strs

  • No interfaces or ABCs for functionality

  • Only covers the root filesystem

pathlib

pathlib hopes to carve a path towards greatness through PEP 428.

Pros

  • Convenient __div__() overloading

Cons

  • No interfaces or ABCs for functionality

  • Only covers the root filesystem

Examples

Saving Data to Disk

The Old Way

def save(base, fragments, data):
    # `fragments` could contain unsafe paths!
    if ".." in fragments or "." in fragments:
        raise ValueError("Unsafe paths!")
    path = os.path.join(os.path.abspath(base), os.sep.join(fragments))
    # Alternatively: path = os.path.join(os.path.abspath(base), *fragments)
    # I hope that this doesn't fail mid-write! Also, did the directories
    # exist? I think so, yes.
    with open(path, "wb") as handle:
        handle.write(data)

The New Way

def save(base, fragments, data):
    path = base.descendant(fragments)
    path.parent().makeDirs()
    path.setContent(data)

Changelog

0.2

  • Initial release

Metadata

Release files for betterpath 0.2.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 betterpath 0.2.2
File Size Uploaded
betterpath-0.2.2.tar.gz 38.1 kB Details

Release files / betterpath-0.2.2.tar.gz

Download URL betterpath-0.2.2.tar.gz
Size 38.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e8d04e884d404dae3e622248e1b9c4b80876e21cd1fbdd76fd50ee0d2cb26fe4
BLAKE2b-256 checksum
How to use checksums
7460be35047cf73c77b76aeee08de0a729b745f9f3e6c7393670115f52597e81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2

1 release file

0.1.dev26

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