Skip to content

Instantly share code, notes, and snippets.

@opplatek
Created July 21, 2022 16:03
Show Gist options
  • Save opplatek/016a4631d834453e28d3a28e6d94f66b to your computer and use it in GitHub Desktop.
Save opplatek/016a4631d834453e28d3a28e6d94f66b to your computer and use it in GitHub Desktop.
Regular input or stdin/stdout decision for Python3 script
#!/usr/bin/python3
import sys
# If intab (input) is defined, read it; if not assume stdin
if intab:
f = open(intab, "r")
else:
f = sys.stdin
# If otab (output) is defined, read it; if not assume stdout
if otab:
fout = open(otab, "w")
else:
fout = sys.stdout
# Do whatever you want
for line in f:
print(line)
write.fout(line)
# And close files, just to be safe
if f is not sys.stdin:
f.close()
if fout is not sys.stdout:
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment