2

This answer says it's as simple as:

pipe python logging stdout stream output to grep

python main.py 2>&1 | grep INFO

I have the following file, which I've tried with print() and sys.stdout.write().

import sys
from time import sleep

while True:
    sleep(1)
    sys.stdout.write("this is a thing")
    # Also tried print()

I'm trying to collect "thing" with:

python output.py 2>&1 | grep "thing"

Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111

3 Answers3

2

You need to use the newline and better to flush the stdout.

import sys
from time import sleep

while True:
    sys.stdout.write("this is a thing\n")
    sys.stdout.flush()
    sleep(1)
han solo
  • 6,390
  • 1
  • 15
  • 19
1
while True:
    sleep(1)
    print("this is a thing", flush=True)

Will work as well, no need to hustle with stdout directly.

By default print uses buffer for output, just by flushing it every call you'll have semi-realtime streaming.

Slam
  • 8,112
  • 1
  • 36
  • 44
0

as an addition: if the output would have gone to stderr, whats not the case here, you would still see it in the terminal, because the pipe | only redirects stdout if you don't specify otherwise the rest goes unfiltered to the terminal

Funksen
  • 111
  • 1
  • 5