# Redirecting stderr

**URL:** https://community.materialise.com/t/redirecting-stderr/622
**Category:** MIS - Scripting Forum
**Tags:** mimics
**Created:** [March 25, 2022, 12:55pm UTC](https://community.materialise.com/t/redirecting-stderr/622 "2022-03-25T12:55:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Thijs](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.materialise.com/thijs/32/53_2.png) [@Thijs](https://community.materialise.com/u/Thijs)
#### Post date: [March 25, 2022, 12:55pm UTC](https://community.materialise.com/t/redirecting-stderr/622/1 "2022-03-25T12:55:33Z")

</div>

Hi,  
I am trying to run a mimics script in the background and I want to redirect the standard output and the errors to a file. I am able to redirect stdout to a file, but I have no such luck with stderr:

```python
import sys
sys.stdout = open("stdout.txt", "w") 
print("Test standard output") # This works in mimics
sys.stderr = open("stderr.txt", "w")
raise ValueError("Test error") # This does not work in mimics

```

When I print something, it does not appear in the console as expected, and the line is written to the file (after flushing).  
However, when I induce an error, the error still appears on the console even though the stderr should have been redirected to the file. Does mimics still intercept stderr somehow? And is there a way to still capture stderr?  
I am using an older version of Mimics (21) but I am currently not able to upgrade.

---

<div class="post-metadata">

### Author: ![Thijs](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.materialise.com/thijs/32/53_2.png) [@Thijs](https://community.materialise.com/u/Thijs)
#### Post date: [March 25, 2022, 1:58pm UTC](https://community.materialise.com/t/redirecting-stderr/622/2 "2022-03-25T13:58:12Z")

</div>

I eventually managed with the following:

```python
import traceback
with open("stderr.txt", "w") as stderr:
    try:
        do_stuff()
        raise ValueError("Test error")
    except:
        stderr.write(traceback.format_exc())

```

It is not the prettiest solution in my opinion.  
If there is a better way to properly redirect stderr with mimics then I’d love to know about it!
