David Y.
—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']
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.