tags | read lines from stdin
using fileinput
$ cat count_lines.py import fileinput with fileinput.input() as f: count = 0 for line in f: count += 1 print('counted ', count, ' lines.')
$ python ./count_lines.py < ./count_lines.py counted 8 lines.
$ wc -l ./count_lines.py 8 ./count_lines.py
using sys.stdin
$ cat count_lines.py import sys data = sys.stdin.readlines() print("Counted", len(data), "lines.")
$ cat count_lines.py | python count_lines.py Counted 3 lines.
Ref:-