capture stdout

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

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

I tried using something like ’

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        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… ¯_(ツ)_/¯ 🤷

I found this post which attempts to explain it.

1 Like