Skip to content

Instantly share code, notes, and snippets.

@iiSeymour
Created May 11, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iiSeymour/81fedd35371fe9bcc916 to your computer and use it in GitHub Desktop.
Save iiSeymour/81fedd35371fe9bcc916 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Short Python example of why `ls | wc -l` works by @iiSeymour
#
# try:
# python isatty.py
# and:
# python isatty.py | cat
#
# Explanation http://www.ginac.de/~kreckel/fileno/
import os
import sys
output = ['I', 'am', 'writing', 'to', 'a']
if os.isatty(sys.stdout.fileno()):
output.append('terminal')
sep = ' '
else:
output.append('pipe')
sep = '\n'
print sep.join(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment