Skip to main content

Ability to fetch resources similar to Java Class.getResource()

Project description

Local Resources

This project is easy attempt to emulate Java's Class.getResource() method.

It provides simple access to files that are 'reachable' from directories and/or zip files referenced in PYTHONPATH.

Usage

Example:

from local_resources import Resource

with Resource("VERSION") as f:
    version = f.read()

It returns read-only file object in binary mode.

Aside of reading knowing files it provides 'list' method which is similar to os.listdir(path) with one caveat - directories are always returned with '/' appended:

from local_resources import Resource

files_and_dirs = Resource("").list()

for f in files_and_dirs:
    if f.endswith("myfiles/to_print_on_screen"):
        print(f"Do something with path {f}")
    else:
        with Resource(f) as file:
            print(f"File {f}:")
            print(file.read().decode('utf-8'))

Note: when list()' method is called there are no resources held after it finished, so there is no need for with` statement, nor cleaning up/closing resources at the end.

Note: for root of PYTHONPATH directories use """ or "/": Resource("").list().

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

local_resources-1.0.2.tar.gz (6.7 kB view hashes)

Uploaded Source

Built Distribution

local_resources-1.0.2-py3-none-any.whl (7.7 kB view hashes)

Uploaded Python 3

Supported by

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