# Saving table from command line

**URL:** <https://discourse.slicer.org/t/saving-table-from-command-line/25256>\
**Category:** Support\
**Tags:** python\
**Created:** [September 14, 2022, 10:13am UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256 "2022-09-14T10:13:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukasvanderstricht](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lukasvanderstricht](https://discourse.slicer.org/u/lukasvanderstricht)\
**Post date:** [September 14, 2022, 10:13am UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/1 "2022-09-14T10:13:26Z")

</div>

Hi everyone,

I have made a lung segmentation on an MRI and used the `Segment Statistics` module to create a table with measurements about the segmentations (such as the volume of the segments). I know how to save this table as a `.csv` file by going to `Save` at the top of the Slicer window.

However, I would want to automate certain of the processes I go through, such as the saving of this table. I wondered if there was anything I could do from command line or with I have found some existing blogposts about how to do this, for example this one: [How to programmatically export data from segment statistics?](https://discourse.slicer.org/t/how-to-programmatically-export-data-from-segment-statistics/17390).

I could however not work out how to do this. I don’t know where I have to run this command. I am working in Windows and I tried running it in a cmd terminal, but to no avail. I explored my filesystem a bit and found some directories that according to me allude to the path that is used in the command in the blogpost. I can navigate to `AppData/Local/NA-MIC/Slicer 5.0.3/bin/Python/slicer` and this directory indeed contains the file `util.py`.

I do not know however how to go about saving the table now? What terminal should I run the command in?

Thank you a lot in advance!

Kind regards

Lukas

---

<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:** [September 14, 2022, 11:31am UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/2 "2022-09-14T11:31:28Z")

</div>

You should run that python code using the Python Interactor in the 3D Slicer application. You can toggle it to show/hide by a python button in the toolbar area, View-\>Python Interactor, or by keyboard shortcuts Ctrl+3/Ctrl+`

---

<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:** [September 14, 2022, 2:09pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/4 "2022-09-14T14:09:11Z")

</div>

> [@lukasvanderstricht](#):
>
> ```auto
> tablenode = getNode('T')
> slicer.util.getNode(tablenode,"test.csv")
> 
> ```

Make sure to use `saveNode` to save the file. In your code above you used `getNode`. Also if you just specify the filename and not the full absolute path it will attempt to save in some last known position or working directly I believe. To be more explicit to where you save, specify the full path. It would be as follows.

```python
tablenode = getNode('T')
slicer.util.saveNode(tablenode, "C:/Users/James/Downloads/Table.csv")

```

---

<div class="post-metadata">

**Author:** ![lukasvanderstricht](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lukasvanderstricht](https://discourse.slicer.org/u/lukasvanderstricht)\
**Post date:** [September 14, 2022, 2:47pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/5 "2022-09-14T14:47:41Z")

</div>

Yes, sorry that was a typo of mine. I indeed found the files I was looking for now.  
I was wondering if I could accomplish the same result by using an external python file. I made such a file with only the following contents:

```auto
def saveTable():
	tablenode = slicer.util.getNode('T')
	slicer.util.saveNode(tablenode,"grap.csv")

```

I loaded this python file into Slicer’s Python environment with `sys.path.append(filepath)` and then `import`. This all seems to work without problems. If I now want to execute the function above (with the command `file.saveTable()`), I get the following error:

> Traceback (most recent call last):  
> File “”, line 1, in   
> File “C:\Users\lukas\OneDrive\Documenten\saveAsGrap.py”, line 2, in saveTable  
> tablenode = slicer.util.getNode(‘T’)  
> NameError: name ‘getNode’ is not defined

Is there any way around this?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 14, 2022, 2:54pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/6 "2022-09-14T14:54:56Z")

</div>

See the notes linked below. `slicer.util` is imported to the interactive console as a convenience.

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#get-mrml-node-from-the-scene](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-mrml-node-from-the-scene)

---

<div class="post-metadata">

**Author:** ![lukasvanderstricht](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lukasvanderstricht](https://discourse.slicer.org/u/lukasvanderstricht)\
**Post date:** [September 14, 2022, 3:00pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/7 "2022-09-14T15:00:23Z")

</div>

Hi, thank you for your response!

However, I don’t see how this can help me to solve my issue.  
Sorry for the inconvienence, I’m a beginner at Slicer.

Thank you!

---

<div class="post-metadata">

**Author:** ![lukasvanderstricht](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lukasvanderstricht](https://discourse.slicer.org/u/lukasvanderstricht)\
**Post date:** [September 14, 2022, 3:02pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/8 "2022-09-14T15:02:04Z")

</div>

If I remove the `slicer.util`-part in front of `getNode`, I still have the same issue.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 14, 2022, 3:10pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/9 "2022-09-14T15:10:41Z")

</div>

Try running using this form instead:

`exec(open(f"{path_to_script}").read())`

---

<div class="post-metadata">

**Author:** ![lukasvanderstricht](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lukasvanderstricht](https://discourse.slicer.org/u/lukasvanderstricht)\
**Post date:** [September 14, 2022, 3:15pm UTC](https://discourse.slicer.org/t/saving-table-from-command-line/25256/10 "2022-09-14T15:15:03Z")

</div>

Hi, thank you for your answer!

This indeed seems to work.

Thank you very much!
