简体   繁体   中英

Grep python output stream (tried print and stdout)

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"

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)
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM