Warm tip: This article is reproduced from stackoverflow.com, please click
pipe python-3.x stdin

In Python how do I read data from stdin until piped process terminates?

发布于 2020-03-27 15:39:43

I'd like to do something like this:

data = json.load(open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin)

which for this example can be reduced to

data = json.load(sys.stdin)

But json.load runs a single .read() which might not contain the whole JSON string I provided via

generate_json_output | myprogram.py

Is there a recommended way to read from stdin until generate_json_output terminates? I tried to catch BrokenPipeError but it doesn't get raised when I run

while True:
    data += sys.stdin.read()
Questioner
frans
Viewed
67
6,021 2020-01-31 16:24

Instead of a while loop, you can use for loop like below to iterate over stdin.

import sys

data = ''
for line in sys.stdin:
    data += line

which can be written as one expression:

data = "".join(sys.stdin)

Running this with input generated using a pipe reads the stdin until it terminates.

t.json:

{
    'a': 2,
    'b': 3
}

Running cat t.json | python a.py prints the complete file to stdout.