Support for pathlib

Hi all,

Now that Python 3 works with Slicer, are there any plans to add support for the standard pathlib library? Currently I just cast all the paths to str before feeding them to Slicer functions:

Old style:

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:

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
pandas
numpy
PIL
matplotlib

Let me know if I can help.

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 (for updating VTK’s Python wrapper), and submit an issue to PythonQt project (for updating Qt wrapping).

1 Like

Update on this:

  1. PythonQt seems to be ok with pathlib: Feature request: add support for pathlib · Issue #38 · MeVisLab/pythonqt · GitHub
  2. Support in VTK seems trickier: Support for pathlib - Development - VTK