# Bug: slicer.util.loadVolume does not accept pathlib Path (only string)

**URL:** <https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820>\
**Category:** Development\
**Tags:** python\
**Created:** [December 19, 2022, 9:26am UTC](https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820 "2022-12-19T09:26:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alistair\_McCutcheon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alistair_mccutcheon/32/13980_2.png) [@Alistair\_McCutcheon](https://discourse.slicer.org/u/Alistair_McCutcheon)\
**Post date:** [December 19, 2022, 9:26am UTC](https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820/1 "2022-12-19T09:26:02Z")

</div>

My slicer is version 5.2.1

This runs successfully:

```auto
slicer.util.loadVolume(str(pathlib.Path(segmentation_dir_path) / "img_0.nii.gz"))

```

With the following message:

```auto
Loaded volume from file: /home/redacted/redacted/redacted/redacted/redacted/redacted/redacted/Public_Patient0/img_0.nii.gz. Dimensions: 200x249x292. Number of components: 1. Pixel type: float.

"Volume" Reader has successfully read the file "/home/redacted/redacted/redacted/redacted/redacted/redacted/redacted/Public_Patient0/img_0.nii.gz" "[0.20s]"

```

This crashes:

```auto
slicer.util.loadVolume(pathlib.Path(segmentation_dir_path) / "img_0.nii.gz")

```

With the following stacktrace:

```auto
Traceback (most recent call last):
  File "/home/redacted/redacted/redacted/Main/Main.py", line 325, in onLoadSegmentationDirectory
    slicer.util.loadVolume(segmentation_dir_path / "img_0.nii.gz")
  File "/opt/Slicer-5.2.1-linux-amd64/bin/Python/slicer/util.py", line 937, in loadVolume
    return loadNodeFromFile(filename, filetype, properties, returnNode)
  File "/opt/Slicer-5.2.1-linux-amd64/bin/Python/slicer/util.py", line 699, in loadNodeFromFile
    raise RuntimeError(errorMessage)
RuntimeError: Failed to load node from file: /home/redacted/redacted/redacted/redacted/redacted/redacted/redacted/Public_Patient0/img_0.nii.gz
Error: Loading - load failed.

```

---

<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:** [December 19, 2022, 7:16pm UTC](https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820/2 "2022-12-19T19:16:43Z")

</div>

By crash do you mean that the application actually crashed, or just the image was not loaded?

`pathlib.Path` objects are not supported yet in Slicer’s Python API. It would require much more work for Slicer developers to support it than for users to add the `str()`, so it has not been high on the priority list. I’ve added an issue to keep track of this request:

> <https://github.com/Slicer/Slicer/issues/6741>
>
> \## Is your feature request related to a problem? Please describe.
> 
> Developers …would like to use pathlib.Path directly as input for Slicer API calls where a path-like object is expected.
> 
> \## Describe the solution you'd like
> 
> Allow developers to pass paths as pathlib.Path object.
> 
> \## Additional context
> 
> https://discourse.slicer.org/t/bug-slicer-util-loadvolume-crashes-when-given-a-pathlib-path-instead-of-a-string/26820

Would you consider contributing to Slicer by adding `pathlib.Path` support to the methods that you are using, for example for [loadVolume](https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/util.py#L918)?

---

<div class="post-metadata">

**Author:** ![Alistair\_McCutcheon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alistair_mccutcheon/32/13980_2.png) [@Alistair\_McCutcheon](https://discourse.slicer.org/u/Alistair_McCutcheon)\
**Post date:** [December 21, 2022, 12:24am UTC](https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820/3 "2022-12-21T00:24:42Z")

</div>

The image was just not loaded (ie, the entire program did NOT crash), sorry for not being clear.

I’ve submitted a pull requested with the change to support pathlib.Path (more generally, it will assume the **str** method of the object will return the path). I’ve done this for both loadNodeFromFile and loadNodesFromFile (loadVolume calls loadNodeFromFile).

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 21, 2022, 2:45am UTC](https://discourse.slicer.org/t/bug-slicer-util-loadvolume-does-not-accept-pathlib-path-only-string/26820/4 "2022-12-21T02:45:53Z")

</div>

> [@Alistair\_McCutcheon](#):
>
> I’ve submitted a pull requested with the change to support pathlib.Path (more generally, it will assume the **str** method of the object will return the path)

Here is the link to the mentioned pull request:

> <https://github.com/Slicer/Slicer/pull/6743>
>
> Issue: https://github.com/Slicer/Slicer/issues/6741
> Topic: https://discourse.sl…icer.org/t/bug-slicer-util-loadvolume-crashes-when-given-a-pathlib-path-instead-of-a-string/26820
