read_from_stdin
Table of Contents
read from stdin
tags | read lines from stdin
approach 1
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
approach 2
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:-
- Used this technique to answer https://unix.stackexchange.com/questions/743855/convert-only-all-uppercase-lines-to-lower-case
read_from_stdin.txt · Last modified: 2024/12/19 21:46 by raju