Created
May 11, 2015 21:38
-
-
Save iiSeymour/81fedd35371fe9bcc916 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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