![]() |
VOOZH | about |
The fileinput.input() function in Python is used to read lines from one or multiple files. It returns an iterable object that allows to process file content line by line. If no file is specified, it reads input from standard input (stdin).
Below is the sample.txt file used in this article, click here to download.
Example: In this example, a file is read line by line using fileinput.input() and its content is printed.
Output
Explanation:
fileinput.input(files=None)
Example 1: In this example, we read content from multiple files and display them together.
Output
Explanation:
Example 2: In this example, we count the total number of lines from a file using fileinput.input().
Output
2
Explanation:
Example 3: In this example, we display the line number along with each line from a file using fileinput.input().
Output
Explanation: