Skip to content

Instantly share code, notes, and snippets.

@puckvg
Created January 27, 2021 11:20
Show Gist options
  • Save puckvg/72eb7b85bee5beca67ba42b92f9ecfe3 to your computer and use it in GitHub Desktop.
Save puckvg/72eb7b85bee5beca67ba42b92f9ecfe3 to your computer and use it in GitHub Desktop.
read stdin in python
3
C 0. 0. 0.
H 1. 1. 1.
H 2. 3. 1.
3
S 1. 1. 1.
H 2. 1. 1.
H 1. 5. 2.
5
C 0. 0. 0.
C 1. 0. 0.
H 1. 2. 1.
H 2. 1. 3.
H 0. 0. 0.
"""
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