Filipe Laíns <la...@riseup.net> added the comment:

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

----------
nosy: +FFY00, jaraco

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45427>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to