1748

How do I read from stdin? Some code golf challenges require using stdin for input.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
tehryan
  • 24,405
  • 11
  • 28
  • 17

25 Answers25

1130

Use the fileinput module:

import fileinput

for line in fileinput.input():
    pass

fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.

Note: line will contain a trailing newline; to remove it use line.rstrip().

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
  • 7
    what's difference between `input()` and `fileinput.input()`? – Amir reza Riahi Mar 21 '21 at 10:51
  • 9
    @AmirrezaRiahi `input()` reads a single line from stdin, whereas `fileinput.input()` will loop through *all* the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided – Boris Verkhovskiy Apr 08 '21 at 15:14
  • Thanks for asking this. I was stuck when one of the fileinput.input() lines was my a command line arg when I wanted to just read stdin. – Sridhar Sarnobat Dec 13 '21 at 04:20
  • why not use input? Otherwise you are in a for loop, while I only want one input. – Thorsten Staerk Jan 27 '22 at 06:50
  • 3
    if you want the lines as a list you can use `list(fileinput.input())` – storm_m2138 Jul 07 '22 at 22:27
  • And what is the difference between `fileinput.input()` and `sys.stdin`? – Martin Braun Oct 23 '22 at 20:22
  • `sys.stdin` is the file handle that `input` reads from and what `fileinput.input()` reads from if there are not command-line arguments. – chepner Nov 18 '22 at 19:52
  • The `fileinput` module is more about providing a single interface to reading from *either* standard input *or* named files, depending on what the user provides. – chepner Nov 18 '22 at 19:53
  • Since all lines will contain a newline you can also remove it with `line[:-1]`. (Shorter, so better for CodeGolf.) – anderium May 18 '23 at 22:24
821

There's a few ways to do it.

  • sys.stdin is a file-like object on which you can call functions read or readlines if you want to read everything or you want to read everything and split it by newline automatically. (You need to import sys for this to work.)

  • If you want to prompt the user for input, you can use raw_input in Python 2.X, and just input in Python 3.

  • If you actually just want to read command-line options, you can access them via the sys.argv list.

You will probably find this Wikibook article on I/O in Python to be a useful reference as well.

dynamicwebpaige
  • 373
  • 1
  • 10
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
585
import sys

for line in sys.stdin:
    print(line)

Note that this will include a newline character at the end. To remove the newline at the end, use line.rstrip() as @brittohalloran said.

phoenix
  • 7,988
  • 6
  • 39
  • 45
user303110
  • 5,976
  • 1
  • 16
  • 4
278

Python also has built-in functions input() and raw_input(). See the Python documentation under Built-in Functions.

For example,

name = raw_input("Enter your name: ")   # Python 2.x

or

name = input("Enter your name: ")   # Python 3
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Pat Notz
  • 208,672
  • 30
  • 90
  • 92
  • 8
    This reads a single line, which isn't really what the OP asked about. I interpret the question as "how do I read a bunch of lines from an open file handle until EOF?" – tripleee Dec 22 '15 at 08:51
  • 5
    The OP isn't asking to read input from a keyboard, He is asking to read from stdin which in a contest situation is usually provided to the contestants. – chrisfs Apr 22 '18 at 21:45
  • 4
    this is what i needed, google brought me here. interestingly i managed to code rfid tags, datetime, databases, but never bothered to read input from the user lol – clockw0rk Jul 16 '19 at 13:44
246

Here's from Learning Python:

import sys
data = sys.stdin.readlines()
print "Counted", len(data), "lines."

On Unix, you could test it by doing something like:

% cat countlines.py | python countlines.py 
Counted 3 lines.

On Windows or DOS, you'd do:

C:\> type countlines.py | python countlines.py 
Counted 3 lines.
Palec
  • 12,743
  • 8
  • 69
  • 138
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • 4
    Here's a more memory efficient (and maybe faster) way to count lines in Python: `print(sum(chunk.count('\n') for chunk in iter(partial(sys.stdin.read, 1 << 15), '')))`. [see `wc-l.py`](http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python#comment11966378_9371238) – jfs Nov 16 '12 at 04:32
  • 17
    The use of `cat` here is redundant. The correct invocation for Unix systems is `python countlines.py < countlines.py`. – istepaniuk Nov 17 '15 at 11:14
  • 21
    "Learning Python" is wrong in directing users to use `readlines()`. File objects are intended to be iterated over without materializing all of the data in memory. – Russia Must Remove Putin Aug 01 '16 at 14:56
  • 4
    @istepaniuk Regarding "use of cat", I find using `cat filespec | filters` more convenient in general when I am tweaking command line parameters to the filters, since they will be at the end of the line each time. – GeePokey May 18 '21 at 21:55
  • 2
    @GeePokey The input redirection can also be placed infront: `< filespec filters` – Panki Oct 27 '21 at 13:38
188

How do you read from stdin in Python?

I'm trying to do some of the code golf challenges, but they all require the input to be taken from stdin. How do I get that in Python?

You can use:

  • sys.stdin - A file-like object - call sys.stdin.read() to read everything.
  • input(prompt) - pass it an optional prompt to output, it reads from stdin up to the first newline, which it strips. You'd have to do this repeatedly to get more lines, at the end of the input it raises EOFError. (Probably not great for golfing.) In Python 2, this is raw_input(prompt).
  • open(0).read() - In Python 3, the builtin function open accepts file descriptors (integers representing operating system IO resources), and 0 is the descriptor of stdin. It returns a file-like object like sys.stdin - probably your best bet for golfing. In Python 2, this is io.open.
  • open('/dev/stdin').read() - similar to open(0), works on Python 2 and 3, but not on Windows (or even Cygwin).
  • fileinput.input() - returns an iterator over lines in all files listed in sys.argv[1:], or stdin if not given. Use like ''.join(fileinput.input()).

Both sys and fileinput must be imported, respectively, of course.

Quick sys.stdin examples compatible with Python 2 and 3, Windows, Unix

You just need to read from sys.stdin, for example, if you pipe data to stdin:

$ echo foo | python -c "import sys; print(sys.stdin.read())"
foo

We can see that sys.stdin is in default text mode:

>>> import sys
>>> sys.stdin
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>

file example

Say you have a file, inputs.txt, we can accept that file and write it back out:

python -c "import sys; sys.stdout.write(sys.stdin.read())" < inputs.txt

Longer answer

Here's a complete, easily replicable demo, using two methods, the builtin function, input (use raw_input in Python 2), and sys.stdin. The data is unmodified, so the processing is a non-operation.

To begin with, let's create a file for inputs:

$ python -c "print('foo\nbar\nbaz')" > inputs.txt

And using the code we've already seen, we can check that we've created the file:

$ python -c "import sys; sys.stdout.write(sys.stdin.read())" < inputs.txt 
foo
bar
baz

Here's the help on sys.stdin.read from Python 3:

read(size=-1, /) method of _io.TextIOWrapper instance
    Read at most n characters from stream.
    
    Read from underlying buffer until we have n characters or we hit EOF.
    If n is negative or omitted, read until EOF.

Builtin function, input (raw_input in Python 2)

The builtin function input reads from standard input up to a newline, which is stripped (complementing print, which adds a newline by default.) This occurs until it gets EOF (End Of File), at which point it raises EOFError.

Thus, here's how you can use input in Python 3 (or raw_input in Python 2) to read from stdin - so we create a Python module we call stdindemo.py:

$ python -c "print('try:\n    while True:\n        print(input())\nexcept EOFError:\n    pass')" > stdindemo.py 

And let's print it back out to ensure it's as we expect:

$ python -c "import sys; sys.stdout.write(sys.stdin.read())" < stdindemo.py 
try:
    while True:
        print(input())
except EOFError:
    pass

Again, input reads up until the newline and essentially strips it from the line. print adds a newline. So while they both modify the input, their modifications cancel. (So they are essentially each other's complement.)

And when input gets the end-of-file character, it raises EOFError, which we ignore and then exit from the program.

And on Linux/Unix, we can pipe from cat:

$ cat inputs.txt | python -m stdindemo
foo
bar
baz

Or we can just redirect the file from stdin:

$ python -m stdindemo < inputs.txt 
foo
bar
baz

We can also execute the module as a script:

$ python stdindemo.py < inputs.txt 
foo
bar
baz

Here's the help on the builtin input from Python 3:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.

sys.stdin

Here we make a demo script using sys.stdin. The efficient way to iterate over a file-like object is to use the file-like object as an iterator. The complementary method to write to stdout from this input is to simply use sys.stdout.write:

$ python -c "print('import sys\nfor line in sys.stdin:\n    sys.stdout.write(line)')" > stdindemo2.py

Print it back out to make sure it looks right:

$ python -c "import sys; sys.stdout.write(sys.stdin.read())" < stdindemo2.py 
import sys
for line in sys.stdin:
    sys.stdout.write(line)

And redirecting the inputs into the file:

$ python -m stdindemo2 < inputs.txt
foo
bar
baz

Golfed into a command:

$ python -c "import sys; sys.stdout.write(sys.stdin.read())" < inputs.txt
foo
bar
baz

File Descriptors for Golfing

Since the file descriptors for stdin and stdout are 0 and 1 respectively, we can also pass those to open in Python 3 (not 2, and note that we still need the 'w' for writing to stdout).

If this works on your system, it will shave off more characters.

$ python -c "open(1,'w').write(open(0).read())" < inputs.txt
baz
bar
foo

Python 2's io.open does this as well, but the import takes a lot more space:

$ python -c "from io import open; open(1,'w').write(open(0).read())" < inputs.txt 
foo
bar
baz

Addressing other comments and answers

One comment suggests ''.join(sys.stdin) for golfing but that's actually longer than sys.stdin.read() - plus Python must create an extra list in memory (that's how str.join works when not given a list) - for contrast:

''.join(sys.stdin)
sys.stdin.read()

The top answer suggests:

import fileinput

for line in fileinput.input():
    pass

But, since sys.stdin implements the file API, including the iterator protocol, that's just the same as this:

import sys

for line in sys.stdin:
    pass

Another answer does suggest this. Just remember that if you do it in an interpreter, you'll need to do Ctrl-d if you're on Linux or Mac, or Ctrl-z on Windows (after Enter) to send the end-of-file character to the process. Also, that answer suggests print(line) - which adds a '\n' to the end - use print(line, end='') instead (if in Python 2, you'll need from __future__ import print_function).

The real use-case for fileinput is for reading in a series of files.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • +1 for `open(0)`. I have an `open(filename)` but with a command line parameter I want to use `stdin` as file. So I do not want to write extra code. – Chameleon Dec 25 '22 at 14:18
105

The answer proposed by others:

for line in sys.stdin:
  print line

is very simple and pythonic, but it must be noted that the script will wait until EOF before starting to iterate on the lines of input.

This means that tail -f error_log | myscript.py will not process lines as expected.

The correct script for such a use case would be:

while 1:
    try:
        line = sys.stdin.readline()
    except KeyboardInterrupt:
        break

    if not line:
        break

    print line

UPDATE
From the comments it has been cleared that on python 2 only there might be buffering involved, so that you end up waiting for the buffer to fill or EOF before the print call is issued.

Massimiliano Torromeo
  • 1,801
  • 1
  • 15
  • 17
  • 9
    The `for line in sys.stdin:` pattern __does not__ wait for EOF. But if you test on very small files, responses may get buffered. Test with more data to see that it reads intermediate results. – mb. Jul 11 '12 at 02:06
  • I get wait for End Of File or buffering, when taking input from a stream when using python 2.6.6, but with 3.1.3 I don't. Note `print line` does not woke in 3.1.3, but `print(line)` does. – ctrl-alt-delor Sep 07 '12 at 09:41
  • my python 2.7.5 "for line in sys.stdin", blocks till EOF or some reasonable amount of data has buffered. Fine for stream processing. Not fine for line by line processing or user input. – Sean Oct 15 '13 at 11:34
  • 2
    I suspect this is related to detection of tty in libc, so when you pipe it detects on a interactive shell it detects none tty, unbuffer from expect-dev is a handy util that I believe injects a shim via ld_preload so is_atty returns true (I suspect that's how it is handing it) – Mâtt Frëëman Mar 12 '15 at 08:16
  • 9
    @Sean: **wrong**. `for line in sys.stdin:` does not "block till EOF". There is a [read-ahead bug in Python 2](https://bugs.python.org/issue3907) that delays the lines until the corresponding buffer is full. It is a buffering issue that is unrelated to EOF. To workaround, use `for line in iter(sys.stdin.readline, ''):` (use `io.open()` for ordinary files). You don't need it in Python 3. – jfs Jan 23 '16 at 07:38
  • @MattFreeman: it is unrelated to "line- vs. block-buffering depending on whether stdout is tty" in this case. The read-ahead bug in Python 2 delays the input even if `tail` were to flush its output in time. `for line in sys.stdin` works fine on Python 3. – jfs Jan 23 '16 at 07:46
  • `for line in sys.stdin:` is not meant for truly interactive input (e.g. getting input from the terminal after prompts), anyway. In most cases, you should not care whether the input is buffered or not. – musiphil Feb 27 '19 at 18:35
39

This will echo standard input to standard output:

import sys
line = sys.stdin.readline()
while line:
    print line,
    line = sys.stdin.readline()
S. Liu
  • 1,018
  • 2
  • 10
  • 25
rlib
  • 7,444
  • 3
  • 32
  • 40
36

Building on all the anwers using sys.stdin, you can also do something like the following to read from an argument file if at least one argument exists, and fall back to stdin otherwise:

import sys
f = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin    
for line in f:
#     Do your stuff

and use it as either

$ python do-my-stuff.py infile.txt

or

$ cat infile.txt | python do-my-stuff.py

or even

$ python do-my-stuff.py < infile.txt

That would make your Python script behave like many GNU/Unix programs such as cat, grep and sed.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
21

argparse is an easy solution

Example compatible with both Python versions 2 and 3:

#!/usr/bin/python

import argparse
import sys

parser = argparse.ArgumentParser()

parser.add_argument('infile',
                    default=sys.stdin,
                    type=argparse.FileType('r'),
                    nargs='?')

args = parser.parse_args()

data = args.infile.read()

You can run this script in many ways:

1. Using stdin

echo 'foo bar' | ./above-script.py

  or shorter by replacing echo by here string:

./above-script.py <<< 'foo bar'

2. Using a filename argument

echo 'foo bar' > my-file.data
./above-script.py my-file.data

3. Using stdin through the special filename -

echo 'foo bar' | ./above-script.py -
oHo
  • 51,447
  • 27
  • 165
  • 200
  • Here is an answer on what to do, if the input file is compressed: https://stackoverflow.com/a/33621549/778533 One can also do `add_argument('--in'` and then pipe to the script and add `--in -` to the command line. P.S. `in` is not a very good name for a variable/attribute. – tommy.carstensen Dec 04 '18 at 13:59
  • `in` isn't just a bad name for a variable, it is illegal. `args.in.read()` will raise InvalidSyntax error because of the `in` reserved keyword. Can simply rename to `infile` like the python argparse docs do: https://docs.python.org/3/library/argparse.html?highlight=stdin#filetype-objects – Ken Colton Dec 23 '18 at 21:52
  • Thank you @tommy.carstensen for your feedback, I have just improved the answer. Merry Christmas and Happy New Year ;-) – oHo Dec 27 '18 at 21:20
16

The following chip of code will help you (it will read all of stdin blocking unto EOF, into one string):

import sys
input_str = sys.stdin.read()
print input_str.split()
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Chandan Kumar
  • 2,617
  • 2
  • 19
  • 20
12

I am pretty amazed no one had mentioned this hack so far:

python -c "import sys; set(map(sys.stdout.write,sys.stdin))"

in python2 you can drop the set() call, but it would work either way

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
7

Try this:

import sys

print sys.stdin.read().upper()

and check it with:

$ echo "Hello World" | python myFile.py
Bacara
  • 6,903
  • 1
  • 20
  • 21
7

You can read from stdin and then store inputs into "data" as follows:

data = ""
for line in sys.stdin:
    data += line
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
Wei
  • 365
  • 4
  • 2
  • 3
    ["do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b . This optimization is fragile even in CPython"](https://www.python.org/dev/peps/pep-0008/#programming-recommendations) – Russia Must Remove Putin May 02 '17 at 13:29
  • The same thing can be done with `data = sys.stdin.read()`, without the problem of repeated string concatenations. – musiphil Feb 27 '19 at 18:31
7

Read from sys.stdin, but to read binary data on Windows, you need to be extra careful, because sys.stdin there is opened in text mode and it will corrupt \r\n replacing them with \n.

The solution is to set mode to binary if Windows + Python 2 is detected, and on Python 3 use sys.stdin.buffer.

import sys

PY3K = sys.version_info >= (3, 0)

if PY3K:
    source = sys.stdin.buffer
else:
    # Python 2 on Windows opens sys.stdin in text mode, and
    # binary data that read from it becomes corrupted on \r\n
    if sys.platform == "win32":
        # set sys.stdin to binary mode
        import os, msvcrt
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
    source = sys.stdin

b = source.read()
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
7

I use the following method, it returns a string from stdin (I use it for json parsing). It works with pipe and prompt on Windows (not tested on Linux yet). When prompting, two line breaks indicate end of input.

def get_from_stdin():

  lb = 0
  stdin = ''

  for line in sys.stdin:
    if line == "\n":
        lb += 1
        if lb == 2:
            break
    else:
        lb = 0
        stdin += line

  return stdin
Bouni
  • 595
  • 6
  • 12
5

For Python 3 that would be:

# Filename e.g. cat.py
import sys

for line in sys.stdin:
    print(line, end="")

This is basically a simple form of cat(1), since it doesn't add a newline after each line. You can use this (after You marked the file executable using chmod +x cat.py such as:

echo Hello | ./cat.py
AdamKalisz
  • 283
  • 3
  • 12
5

Since Python 3.8 you can use assignment expression:

while (line := input()):
    print(line)
hant0508
  • 528
  • 1
  • 9
  • 19
  • Doesn’t this produce an `EOFError` when stdin ends? I can’t get around that by using `input()`. – Dato Mar 25 '22 at 16:04
  • On Python 3.8 it indeed produces `EOFError: EOF when reading a line`. To get around you can use `try ... except EOFError: pass` – Dereckson Mar 30 '22 at 17:56
4

The problem I have with solution

import sys

for line in sys.stdin:
    print(line)

is that if you don't pass any data to stdin, it will block forever. That's why I love this answer: check if there is some data on stdin first, and then read it. This is what I ended up doing:

import sys
import select

# select(files to read from, files to write to, magic, timeout)
# timeout=0.0 is essential b/c we want to know the asnwer right away
if select.select([sys.stdin], [], [], 0.0)[0]:
    help_file_fragment = sys.stdin.read()
else:
    print("No data passed to stdin", file=sys.stderr)
    sys.exit(2)
Tomas Tomecek
  • 6,226
  • 3
  • 30
  • 26
  • I'd seriously recommend hiding this hideous if condition into a method though. – tiktak Nov 04 '17 at 01:41
  • 1
    This method limits the applicability of the program seriously: for example, you cannot use this for interactive input from the terminal, because the input will almost never be "ready" when `select` is called; or you could also face problems if stdin is connected to a file on a slow medium (network, CD, tape, etc.). You said that "if you don't pass any data to stdin, it will block forever." is a *problem*, but I would say it's a *feature*. Most CLI programs (e.g. `cat`) work this way, and they are expected to. EOF is the only thing you should depend on to detect the end of the input. – musiphil Feb 27 '19 at 18:20
3

When using -c command, as a tricky way, instead of reading the stdin (and more flexible in some cases) you can pass a shell script command as well to your python command by putting the shell command in quotes within a parenthesis started by $ sign.

e.g.

python3 -c "import sys; print(len(sys.argv[1].split('\n')))" "$(cat ~/.goldendict/history)"

This will count the number of lines from goldendict's history file.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    This is clever, I was piping into `python -c` this way and this was an interesting workaround. Thank you for sharing. :) – catleeball Jul 29 '20 at 23:33
2

I had some issues when getting this to work for reading over sockets piped to it. When the socket got closed it started returning empty string in an active loop. So this is my solution to it (which I only tested in linux, but hope it works in all other systems)

import sys, os
sep=os.linesep

while sep == os.linesep:
    data = sys.stdin.readline()               
    sep = data[-len(os.linesep):]
    print '> "%s"' % data.strip()

So if you start listening on a socket it will work properly (e.g. in bash):

while :; do nc -l 12345 | python test.py ; done

And you can call it with telnet or just point a browser to localhost:12345

estani
  • 24,254
  • 2
  • 93
  • 76
2

Regarding this:

for line in sys.stdin:

I just tried it on python 2.7 (following someone else's suggestion) for a very large file, and I don't recommend it, precisely for the reasons mentioned above (nothing happens for a long time).

I ended up with a slightly more pythonic solution (and it works on bigger files):

with open(sys.argv[1], 'r') as f:
    for line in f:

Then I can run the script locally as:

python myscript.py "0 1 2 3 4..." # can be a multi-line string or filename - any std.in input will work
szeitlin
  • 3,197
  • 2
  • 23
  • 19
  • Opening a file is not reading from stdin, like the question asks. -1 – Russia Must Remove Putin Aug 01 '16 at 13:27
  • In this case I am passing in `sys.stdin` as a command-line argument to the script. – szeitlin Aug 01 '16 at 22:56
  • 1
    How could you pass `sys.stdin` as a command-line argument to the script? Arguments are strings and streams are file-like objects, they are not the same. – DeFazer Sep 17 '16 at 02:12
  • @DeFazer edited to show how to use it. Arguments are strings, yes, but as the python docs and I mentioned in an earlier comment above, `sys.stdin` is a file-like object – szeitlin Sep 17 '16 at 09:37
1

There is os.read(0, x) which reads xbytes from 0 which represents stdin. This is an unbuffered read, more low level than sys.stdin.read()

Jay
  • 2,535
  • 3
  • 32
  • 44
1
Nonblocking, bytemode, stdin -> stdout:
# pipe.py

import os, sys, time

os.set_blocking(0, False)
sys.stdin = os.fdopen(0, 'rb', 0)
sys.stdout = os.fdopen(1, 'wb', 0)

while 1:
    time.sleep(.1)
    try: out = sys.stdin.read()
    except:
        sys.stdout.write(b"E")
        continue
    if out is None:
        sys.stdout.write(b"N")
        continue
    if not out:
        sys.stdout.write(b"_")
        break
    # working..
    out = b"<" + out + b">"
    sys.stdout.write(out)

sys.stdout.write(b".\n")
Usage:
$ for i in 1 2 3; do sleep 1; printf "===$i==="; done | python3 pipe.py
NNNNNNNNN<===1===>NNNNNNNNN<===2===>NNNNNNNNN<===3===>_.
Minimal code:
import os, sys

os.set_blocking(0, False)
fd0 = os.fdopen(0, 'rb', 0)
fd1 = os.fdopen(1, 'wb', 0)

while 1:
    bl = fd0.read()
    if bl is None: continue
    if not bl: break
    fd1.write(bl)

Tested on Linux, Python 3.9.2

andronick83
  • 41
  • 1
  • 3
1

Worth saying for short command line chaining input is preferred over fileinput and sys.stdin as it requires no import, and is shorter to type.

$ echo hello world | python3 -c "print(input().upper())"
HELLO WORLD
Meitham
  • 9,178
  • 5
  • 34
  • 45