# Does a Python "reader" need to be closed?

**URL:** https://discourse.vtk.org/t/does-a-python-reader-need-to-be-closed/7261
**Category:** Support
**Created:** [November 24, 2021, 4:27am UTC](https://discourse.vtk.org/t/does-a-python-reader-need-to-be-closed/7261 "2021-11-24T04:27:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![GregVernon](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gregvernon/32/3123_2.png) [@GregVernon](https://discourse.vtk.org/u/GregVernon)
#### Post date: [November 24, 2021, 4:27am UTC](https://discourse.vtk.org/t/does-a-python-reader-need-to-be-closed/7261/1 "2021-11-24T04:27:06Z")

</div>

I’m writing a Python script to serve as the black-box simulation interface driver for a [DAKOTA](https://dakota.sandia.gov/) project. Essentially, this Python driver generates a simulation model, submits a solver subprocess which creates a new `.vtu` file with the hardcoded name: `output_ts000001.vtu`, which is then read using another method that does:

```auto
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName( "output_ts000001.vtu" )
reader.Update()
output = reader.GetOutput()

```

I extract out the data I need, compute, then return the objective function. DAKOTA then generates the next iteration / guess and the process starts over. My question is, do I need to close the reader after I finish processing the data? It would seem like the file would be open, and wouldn’t be able to be overwritten or deleted. Yet I’ve been poking around [https://kitware.github.io/vtk-examples/site/Python](https://kitware.github.io/vtk-examples/site/Python) and [https://gitlab.kitware.com/vtk/vtk-examples/-/tree/master/src/Python/IO](https://gitlab.kitware.com/vtk/vtk-examples/-/tree/master/src/Python/IO) and can’t seem to find any examples that `close()` a reader. This suggests to me that this isn’t necessary, but that clashes with my general I/O experiences. Is calling `del reader` sufficient? Is there some documentation I missed that describes why deleting or closing _isn’t_ necessary?

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [November 24, 2021, 7:16am UTC](https://discourse.vtk.org/t/does-a-python-reader-need-to-be-closed/7261/2 "2021-11-24T07:16:45Z")

</div>

The file stream is destroyed when the reader is destroyed, so there’s no need to close it explicitly.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 24, 2021, 5:42pm UTC](https://discourse.vtk.org/t/does-a-python-reader-need-to-be-closed/7261/3 "2021-11-24T17:42:07Z")

</div>

The reader will close the stream as soon as it finishes reading the data.

In other words, `reader.Update()` will open the file, read its contents, and then close the file.
