How to read input from stdin in Python
This tutorial discussed stdin
the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console.
In Python, fileinput.input()
usestdin
fileinput
We can use the read module
in Python stdin
. fileinput.input()
It reads all the lines of the input file name specified in the command line argument. If no argument is specified, it reads the standard input provided.
The following example shows how to read from a specified input file name.
We will use the following sample.txt
.
Hello
Line1
Line2
Below is the script read.py
.
import fileinput
for line in fileinput.input():
print(line.rstrip())
We execute it like this.
python read.py "sample.txt"
Output:
Hello
Line1
Line2
The following example illustrates reading data from standard input.
import fileinput
for line in fileinput.input():
print("Output:", line.rstrip())
The execution and output are shown below.
python read.py
Line 1
Output: Line 1
Line 2
Output: Line2
^Z
We can save the data for later processing as follows:
import fileinput
data = []
for line in fileinput.input():
data.append(line.rstrip())
Note that we used line.rstrip()
. to delete the trailing newline.
Entering y
will delete all variables.
In Python, sys.stdin
usestdin
Another way is sys.stdin
to read in Python using stdin
. The following example illustrates stdin
reading data line by line from .
import sys
for line in sys.stdin:
print("Output:", line.rstrip())
The execution and output are shown below.
python read.py
Line 1
Output: Line 1
Line 2
Output: Line2
^Z
We can also stdin
read all the data from it at once, instead of reading it line by line.
The following example illustrates this.
import sys
data = sys.stdin.readlines()
data = [line.rstrip() for line in data]
Note that we used line.rstrip()
. This is to remove the trailing newline character.
in conclusion
We discussed stdin
two ways to read input in Python, fileinput.input()
and sys.stdin
. fileinput.input()
Data can be read from file names specified in command line arguments or from standard input, while sys.stdin
can only read data from standard input.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si
Calculating Mahalanobis distance in Python
Publish Date:2025/05/05 Views:185 Category:Python
-
This tutorial will show you how to find the Mahalanobis distance between two NumPy arrays in Python. Use the function scipy.spatial.distance in the Python library cdist() to calculate the Mahalanobis distance The Mahalanobis distance is a m
Implementing the ReLU function in Python
Publish Date:2025/05/05 Views:177 Category:Python
-
This tutorial will discuss the Relu function and how to implement it in Python. ReLU function Relu Functions are the foundation of machine learning and are essential when using deep learning. ReLU The term is Rectified Linear Unit an acrony
Killing a Python process
Publish Date:2025/05/05 Views:152 Category:Python
-
When programming in Python, sometimes our program gets stuck in an infinite loop. In this case, we need to terminate the program manually. This article will discuss different ways to kill a Python process. Killing a Python process using a k
Get file extension in Python
Publish Date:2025/05/05 Views:63 Category:Python
-
This tutorial shows how to get the file extension from a file name in Python. os.path Extract extension from file using module in Python The Python module os.path pre-makes useful utility functions for manipulating operating system file pat