# capture stdout

**URL:** https://discourse.itk.org/t/capture-stdout/2431
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [November 20, 2019, 4:12pm UTC](https://discourse.itk.org/t/capture-stdout/2431 "2019-11-20T16:12:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)
#### Post date: [November 20, 2019, 4:12pm UTC](https://discourse.itk.org/t/capture-stdout/2431/1 "2019-11-20T16:12:23Z")

</div>

I would like to capture stdout for a portion of a command that doesn’t have a direct python interface.

e.g. SimpleElastix has this command  
sitk.PrintParameterMap(rigid)

but I would like to parse the stdout, but I Don’t know how to get at that information.

I tried using something like ’

```auto
class Capturing(list):
    def __enter__ (self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__ (self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio # free up some memory
        sys.stdout = self._stdout

```

but that doesn’t appear to work.  
or maybe there is another way around this?

basically I killed some time just to realize I was trying to solve a hard problem that wasn’t necessary for my application… ¯\_(ツ)\_/¯ 🤷

---

<div class="post-metadata">

### Author: ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)
#### Post date: [November 21, 2019, 3:08am UTC](https://discourse.itk.org/t/capture-stdout/2431/2 "2019-11-21T03:08:44Z")

</div>

I found this post which attempts to explain it.

[https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/#id1](https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/#id1)
