Skip to main content
5 of 6
edited title
user541686
  • 212.5k
  • 133
  • 563
  • 935

Temporarily Redirect stdout/stderr

Is it possible to temporarily redirect stdout/stderr in Python (i.e. for the duration of a method)?

###Edit:

The problem with the current solutions (which I at first remembered but then forgot) is that they don't redirect; rather, they just replace the streams in their entirety. Hence, if a method has a local copy of one the variable for any reason (e.g. because the stream was passed as a parameter to something), it won't work.

Any solutions?

user541686
  • 212.5k
  • 133
  • 563
  • 935