# Support for pathlib

**URL:** <https://discourse.slicer.org/t/support-for-pathlib/9131>\
**Category:** Feature requests\
**Created:** [November 13, 2019, 2:44pm UTC](https://discourse.slicer.org/t/support-for-pathlib/9131 "2019-11-13T14:44:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [November 13, 2019, 2:44pm UTC](https://discourse.slicer.org/t/support-for-pathlib/9131/1 "2019-11-13T14:44:46Z")

</div>

Hi all,

Now that Python 3 works with Slicer, are there any plans to add support for the standard [`pathlib`](https://docs.python.org/3/library/pathlib.html) library? Currently I just cast all the paths to str before feeding them to Slicer functions:

Old style:

```python
import os
myDir = '/tmp/images'
for root, dirs, files in os.walk(myDir, topdown=False):
    for name in files:
        _, ext = os.splitext(name)
        if ext == '.nrrd':
            loadVolume(os.path.join(root, name))

```

New style:

```python
from pathlib import Path
myDir = Path('/tmp/images')
for file in myDir.glob('**/*.nrrd'):
    loadVolume(str(file)) # need str until pathlib is supported

```

Here are some issues from big Python libraries:  
[`nibabel`](https://github.com/nipy/nibabel/pull/610)  
[`pandas`](https://github.com/pandas-dev/pandas/blob/325dd686de1589c17731cf93b649ed5ccb5a99b4/pandas/io/common.py#L131-L160)  
[`numpy`](https://github.com/numpy/numpy/issues/6418)  
[`PIL`](https://github.com/python-pillow/Pillow/issues/1368)  
[`matplotlib`](https://github.com/matplotlib/matplotlib/pull/6788)

Let me know if I can help.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 14, 2019, 2:43am UTC](https://discourse.slicer.org/t/support-for-pathlib/9131/2 "2019-11-14T02:43:41Z")

</div>

It would not be hard to add to slicer.util functions (pull request is welcome), but 99% of the API is C++ wrapped VTK and Qt classes. To add automatic Path\<-\>str conversion to the C++ API, you could ask dgobbi on the [VTK forum](https://discourse.vtk.org) (for updating VTK’s Python wrapper), and submit an issue to [PythonQt](https://github.com/MeVisLab/pythonqt) project (for updating Qt wrapping).

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [February 9, 2021, 2:49pm UTC](https://discourse.slicer.org/t/support-for-pathlib/9131/3 "2021-02-09T14:49:25Z")

</div>

Update on this:

1. PythonQt seems to be ok with `pathlib`: [Feature request: add support for pathlib · Issue #38 · MeVisLab/pythonqt · GitHub](https://github.com/MeVisLab/pythonqt/issues/38)
2. Support in VTK seems trickier: [Support for pathlib - Development - VTK](https://discourse.vtk.org/t/support-for-pathlib/5085/6?u=fepegar)
