Skip to content

Commit cee723e

Browse files
tornariaMatthias Koeppe
authored andcommitted
src/doc/en/developer/coding_basics.rst: Add importlib.resources examples
1 parent 6dbe4db commit cee723e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/doc/en/developer/coding_basics.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ included in one of the following places:
172172
except ImportError:
173173
from importlib.resources import files
174174

175+
After this import, you can:
176+
177+
- open a resource for text reading: `fd = files(package).joinpath(resource).open('rt')`
178+
- open a resource for binary reading: `fd = files(package).joinpath(resource).open('rb')`
179+
- read a resource as text: `text = files(package).joinpath(resource).read_text()`
180+
- read a resource as bytes: `bytes = files(package).joinpath(resource).read_bytes()`
181+
- open a xz resource for text reading: `fd = lzma.open(files(package).joinpath(resource), 'rt')`
182+
- open a xz resource for binary reading: `fd = lzma.open(files(package).joinpath(resource), 'rb')`
183+
175184
If the file needs to be used outside of Python, then the
176185
preferred way is using the context manager
177186
:func:`importlib.resources.as_file`. It should be imported as

0 commit comments

Comments
 (0)