# Support for pathlib

**URL:** https://discourse.vtk.org/t/support-for-pathlib/5085
**Category:** Development
**Created:** [January 28, 2021, 11:17pm UTC](https://discourse.vtk.org/t/support-for-pathlib/5085 "2021-01-28T23:17:47Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![efahl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/efahl/32/1087_2.png) [@efahl](https://discourse.vtk.org/u/efahl)
#### Post date: [January 29, 2021, 4:13pm UTC](https://discourse.vtk.org/t/support-for-pathlib/5085/6 "2021-01-29T16:13:26Z")

</div>

> [@fepegar](#):
>
> `Path.glob` returns `Path` objects, so I’m not sure what you mean. That code works fine on my computer.

Correct for your specific use, but generic `os.PathLike` objects can contain `str` _or_ `bytes`, depending on how you got your `path` object, so if you just do `str(path)`, it could produce an unexpected result. The point of `os.fspath()` is to avoid this issue by looking at the type of `path` and “doing the right thing”.

So, if this goes anywhere, it would be best to do it once in a way that handles not just `pathlib.Path`, but anything that implements the ` __fspath__ ` protocol (I’m thinking specifically of `os.scandir` and its `os.DirEntry` objects).

Sort of an aside…  
Back when PEP 519 was being finalized, I converted a large path handling library to support it, and found that since we dealt with VTK on the back end, I had to further transform the output of `os.fspath()` with something like this, as we were getting some wacky bytes paths for files that had names in, if I remember right, Chinese. This ensured that the C++ code only ever got UTF-8 and made everything happy.

```auto
def _fspath(path: PathLike) -> str:
    _path = os.fspath(path)
    return _path.decode('UTF-8') if isinstance(_path, bytes) else _path

```

---

_[View the full topic](https://discourse.vtk.org/t/support-for-pathlib/5085)._
