The importlib.resources
functions {open,read}_{text,binary}
, path
, is_resource
and contents
, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed by quite a few users.
They provide a simple API for simple tasks, while the full-featured Traversable
API is better suited for complex ones -- especially for implementing new resources-aware loaders.
I'm now in a position where I can add these functions back and support them.
Their main drawback -- not allowing subdirectories -- can be solved by taking multiple path components as positional arguments, for example:
importlib.resources.read_text('modulename', 'subdirectory', 'subsubdir', 'resource.txt')
The additional arguments (encoding and errors) would become keyword-only.
There is a wrinkle in this: in Python 3.9-3.11, the above would mean:
importlib.resources.read_text( 'modulename', 'subdirectory', encoding='subsubdir', errors='resource.txt', )
I believe that this is acceptable, since:
However, if this is a problem, I can
[edit: This is solved by:]
encoding
argument required if a text-reading function more than one path component is given.importlib.resources.read_text( 'modulename', 'subdirectory', 'subsubdir', 'resource.txt', encoding='utf-8', ) importlib.resources.read_text('modulename', 'resource.txt') # OK importlib.resources.read_text('modulename', 'subdirectory', 'utf-8') # errorHas this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386/29
Linked PRsRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4