-3

So I have a python script which I'm redirecting stdout AND stderr to a file. I need to flush this file periodically. What is the best way to achieve this

sys.stdout = sys.stderr = open('location/log.txt', 'w')

Do we have to do it in two separate steps?

sys.stdout.flush()
sys.stderr.flush()

I'm doing this as i'm constantly reading from this file to check for updates.

Macbook
  • 45
  • 1
  • 11
  • I think the word you're looking for is ["**pythonic**"](https://stackoverflow.com/questions/25011078/what-does-pythonic-mean). – Robᵩ Oct 12 '17 at 21:58

1 Answers1

2

If you've aliased sys.stderr and sys.stdout to the same open file handle, you only need to flush one of them; they're the same file object, so flushing one is flushing "all" ("all" being the only existing file object).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271