Message404042
The Traversable protocol does not guarantee you access to the file-system path. pathlib.Path happens to give you that information, but other traversables are not required to.
The main reasoning for this is that traversables do not need to exist on the file-system, we can be reading from a zip, database, etc.
str(path) does give you the file-system path on pathlib.Path, but there is no guarantee about the value of __str__ on other traversables.
My recommendation here would be to use os.fspath[1] instead if you want to try getting the file-system path from traversables.
I don't really know what you are trying to accomplish, but I would recommend that you try designing your code directly on top of the Traversable interface, which should make it work on anything we return in importlib.resources.files.
If you actually need a file-system path, to pass to an external program or something like that, you can use the importlib.resources.as_file[2] helper.
bpo-44200 proposes documenting that traversables should implement __fspath__ if they represent a file-system path, which could help a bit with your issue.
[1] https://docs.python.org/3/library/os.html#os.fspath
[2] https://docs.python.org/3/library/importlib.html#importlib.resources.as_file |
|
Date |
User |
Action |
Args |
2021-10-15 19:01:09 | FFY00 | set | recipients:
+ FFY00, jaraco, eric.smith, daveraja |
2021-10-15 19:01:09 | FFY00 | set | messageid: <1634324469.68.0.917299723335.issue45427@roundup.psfhosted.org> |
2021-10-15 19:01:09 | FFY00 | link | issue45427 messages |
2021-10-15 19:01:09 | FFY00 | create | |
|