In a Python script, how can I read from standard input (stdin
)? For example, I want to prompt the user for their name and use it later on in my code.
Python provides a few methods for reading from stdin
in different contexts. The simplest and most commonly used is the default input
function, which prompts the user to enter a line into stdin
and returns it as a string once they press Enter. For example:
name = input("Enter your name: ") print(f"Hi {name}!")
Running this script will look like this:
$ python main.py Enter your name: Jane Doe Hi Jane Doe!
If we want to read multiple lines from stdin
and don’t need to provide a prompt, we can use sys.stdin
from Python’s built-in sys
module. This allows us to treat stdin
like a file. For example:
import sys for line in sys.stdin: print(f"Received: {line.strip()}")
We could then pipe a file (lines.txt
) into our script:
python stdin_reader.py < lines.txt
For a lines.txt
with the following content:
Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice. The sky above the port was the color of television, tuned to a dead channel. It was a bright cold day in April, and the clocks were striking thirteen.
We would get this output:
Received: Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice. Received: The sky above the port was the color of television, tuned to a dead channel. Received: It was a bright cold day in April, and the clocks were striking thirteen.
As with a file, we could also read all the data at once instead of going line by line, using sys.stdin.read
:
import sys lines = sys.stdin.read() print(f"Received:\n{lines}")
Using lines.txt
again, this would produce the output below:
Received: Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice. The sky above the port was the color of television, tuned to a dead channel. It was a bright cold day in April, and the clocks were striking thirteen.
Note that in both cases, we could enter lines into stdin
interactively as we would with input
instead of piping a file into the script. If we run our script without piped input, it will wait for us to enter something into stdin
. We can then type or paste in multiple lines, finishing with Ctrl-D
:
$ python stdin_reader.py Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice. The sky above the port was the color of television, tuned to a dead channel. It was a bright cold day in April, and the clocks were striking thirteen. ^D Received: Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice. The sky above the port was the color of television, tuned to a dead channel. It was a bright cold day in April, and the clocks were striking thirteen.
Finally, sys.argv
provides a list of command line arguments passed to our script upon execution, starting with the script name. For example, the script below will print a list of its arguments:
import sys print(sys.argv)
We could use it as follows:
$ python args.py 1 2 3 4 ['args.py', '1', '2', '3', '4']
Get actionable, code-level insights to resolve Python performance bottlenecks and errors.
Create a free Sentry account
Create a Python project and note your DSN
Grab the Sentry Python SDK
pip install --upgrade sentry-sdk
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )
Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.