Created
January 27, 2021 11:20
-
-
Save puckvg/72eb7b85bee5beca67ba42b92f9ecfe3 to your computer and use it in GitHub Desktop.
read stdin in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
C 0. 0. 0. | |
H 1. 1. 1. | |
H 2. 3. 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
S 1. 1. 1. | |
H 2. 1. 1. | |
H 1. 5. 2. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 | |
C 0. 0. 0. | |
C 1. 0. 0. | |
H 1. 2. 1. | |
H 2. 1. 3. | |
H 0. 0. 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
read stdin in python, eg find . -name "*.xyz" | python read_stdin.py | |
""" | |
import sys | |
for name in sys.stdin: | |
name = name.strip() | |
# skip if empty string | |
if not name: | |
continue | |
print("name", name) | |
# do something with file | |
with open(name, "r") as f: | |
lines = f.readlines() | |
lines = [line.strip() for line in lines] | |
print("file contains", lines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment